home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Date / Manip.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  125.5 KB  |  3,959 lines

  1. package Date::Manip;
  2.  
  3.  
  4.  
  5. $Date::Manip::GlobalCnf="";
  6. $Date::Manip::IgnoreGlobalCnf="";
  7.  
  8.  
  9. $Date::Manip::PersonalCnf=".DateManip.cnf";
  10. $Date::Manip::PersonalCnfPath=".:~";
  11.  
  12.  
  13. $Date::Manip::Language="English";
  14.  
  15. $Date::Manip::DateFormat="US";
  16.  
  17. $Date::Manip::TZ="";
  18.  
  19. $Date::Manip::ConvTZ="";
  20.  
  21. $Date::Manip::Internal=0;
  22.  
  23. $Date::Manip::FirstDay=0;
  24.  
  25. $Date::Manip::WorkWeekBeg=1;
  26. $Date::Manip::WorkWeekEnd=5;
  27.  
  28. $Date::Manip::WorkDay24Hr=0;
  29.  
  30. $Date::Manip::WorkDayBeg="08:00";
  31. $Date::Manip::WorkDayEnd="17:00";
  32.  
  33. $Date::Manip::EraseHolidays="";
  34.  
  35. $Date::Manip::DeltaSigns=0;
  36.  
  37.  
  38. require 5.000;
  39. require Exporter;
  40. @ISA = qw(Exporter);
  41. @EXPORT = qw(
  42.    DateManipVersion
  43.    ParseDate
  44.    UnixDate
  45.    DateCalc
  46.    ParseDateDelta
  47.  
  48.    Date_SetTime
  49.    Date_GetPrev
  50.    Date_GetNext
  51.    Date_DayOfWeek
  52.    Date_SecsSince1970
  53.    Date_SecsSince1970GMT
  54.    Date_DaysSince999
  55.    Date_DayOfYear
  56.    Date_DaysInYear
  57.    Date_WeekOfYear
  58.    Date_LeapYear
  59.    Date_DaySuffix
  60.    Date_TimeZone
  61.    Date_ConvTZ
  62.    Date_Init
  63.    Date_IsWorkDay
  64.    Date_NextWorkDay
  65.    Date_PrevWorkDay
  66. );
  67. use strict;
  68. use Carp;
  69. use Cwd;
  70.  
  71.  
  72.  
  73.  
  74.  
  75. $Date::Manip::Version="5.10";
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. $Date::Manip::Am = undef;
  124. $Date::Manip::AmExp = undef;
  125. $Date::Manip::AmPmExp = undef;
  126. $Date::Manip::Approx = undef;
  127. $Date::Manip::At = undef;
  128. $Date::Manip::Business = undef;
  129. $Date::Manip::Curr = undef;
  130. $Date::Manip::CurrAmPm = undef;
  131. $Date::Manip::CurrD = undef;
  132. $Date::Manip::CurrH = undef;
  133. $Date::Manip::CurrHolidayYear = 0;
  134. $Date::Manip::CurrM = undef;
  135. $Date::Manip::CurrMn = undef;
  136. $Date::Manip::CurrS = undef;
  137. $Date::Manip::CurrY = undef;
  138. $Date::Manip::CurrZoneExp = undef;
  139. $Date::Manip::DExp = undef;
  140. $Date::Manip::DayExp = undef;
  141. $Date::Manip::Exact = undef;
  142. $Date::Manip::Future = undef;
  143. $Date::Manip::HExp = undef;
  144. $Date::Manip::In = undef;
  145. $Date::Manip::Init = 0;
  146. $Date::Manip::InitDone = 0;
  147. $Date::Manip::InitFilesRead = 0;
  148. $Date::Manip::MExp = undef;
  149. $Date::Manip::MnExp = undef;
  150. $Date::Manip::Mode = undef;
  151. $Date::Manip::MonExp = undef;
  152. $Date::Manip::Next = undef;
  153. $Date::Manip::Now = undef;
  154. $Date::Manip::Offset = undef;
  155. $Date::Manip::On = undef;
  156. $Date::Manip::Past = undef;
  157. $Date::Manip::Pm = undef;
  158. $Date::Manip::PmExp = undef;
  159. $Date::Manip::Prev = undef;
  160. $Date::Manip::ResetWorkDay = 1;
  161. $Date::Manip::SepHM = undef;
  162. $Date::Manip::SepMS = undef;
  163. $Date::Manip::SepSS = undef;
  164. $Date::Manip::SExp = undef;
  165. $Date::Manip::TimesExp = undef;
  166. $Date::Manip::UpdateHolidays = 0;
  167. $Date::Manip::WDBh = undef;
  168. $Date::Manip::WDBm = undef;
  169. $Date::Manip::WDEh = undef;
  170. $Date::Manip::WDEm = undef;
  171. $Date::Manip::WDlen = undef;
  172. $Date::Manip::WExp = undef;
  173. $Date::Manip::WhichExp = undef;
  174. $Date::Manip::WkExp = undef;
  175. $Date::Manip::YExp = undef;
  176. $Date::Manip::ZoneExp = undef;
  177.  
  178. @Date::Manip::Day = ();
  179. @Date::Manip::Mon = ();
  180. @Date::Manip::Month = ();
  181. @Date::Manip::W = ();
  182. @Date::Manip::Week = ();
  183. @Date::Manip::Wk = ();
  184.  
  185. %Date::Manip::AmPm = ();
  186. %Date::Manip::CurrHolidays = ();
  187. %Date::Manip::CurrZone = ();
  188. %Date::Manip::Day = ();
  189. %Date::Manip::Holidays = ();
  190. %Date::Manip::Month = ();
  191. %Date::Manip::Offset = ();
  192. %Date::Manip::Times = ();
  193. %Date::Manip::Replace = ();
  194. %Date::Manip::Week = ();
  195. %Date::Manip::Which = ();
  196. %Date::Manip::Zone = ();
  197.  
  198.  
  199. sub DateManipVersion {
  200.   return $Date::Manip::Version;
  201. }
  202.  
  203. sub Date_SetTime {
  204.   my($date,$h,$mn,$s)=@_;
  205.   &Date_Init()  if (! $Date::Manip::InitDone);
  206.   my($y,$m,$d)=();
  207.  
  208.   if (! &CheckDate($date)) {
  209.     $date=&ParseDate($date);
  210.     return ""  if (! $date);
  211.   }
  212.  
  213.   ($y,$m,$d)=( &CheckDate($date) )[0..2];
  214.   ($h,$mn,$s)=&ParseTime($h,$mn,$s);
  215.  
  216.   my($ampm,$wk);
  217.   return ""  if (&Date_ErrorCheck(\$y,\$m,\$d,\$h,\$mn,\$s,\$ampm,\$wk));
  218.   &FormDate($y,$m,$d,$h,$mn,$s);
  219. }
  220.  
  221. sub Date_GetPrev {
  222.   my($date,$day,$today,$hr,$min,$sec)=@_;
  223.   &Date_Init()  if (! $Date::Manip::InitDone);
  224.   my($y,$m,$d,$h,$mn,$s,$err)=();
  225.  
  226.   if (! &CheckDate($date)) {
  227.     $date=&ParseDate($date);
  228.     return ""  if (! $date);
  229.   }
  230.   ($y,$m,$d)=( &CheckDate($date) )[0..2];
  231.  
  232.   if (defined $day and $day ne "") {
  233.     my($day_w)=();
  234.     my($date_w)=&Date_DayOfWeek($m,$d,$y);
  235.     my(%days)=%Date::Manip::Week;
  236.     if (&IsInt($day)) {
  237.       $day_w=$day;
  238.     } else {
  239.       return ""  if (! exists $days{lc($day)});
  240.       $day_w=$days{lc($day)};
  241.     }
  242.     if ($day_w == $date_w) {
  243.       $date=&DateCalc_DateDelta($date,"-0:0:7:0:0:0",\$err,0)  if (! $today);
  244.     } else {
  245.       $day_w -= 7  if ($day_w>$date_w); # make sure previous day is less
  246.       $day = $date_w - $day_w;
  247.       $date=&DateCalc_DateDelta($date,"-0:0:$day:0:0:0",\$err,0);
  248.     }
  249.     $date=&Date_SetTime($date,$hr,$min,$sec)  if (defined $hr);
  250.  
  251.   } else {
  252.     ($h,$mn,$s)=( &CheckDate($date) )[3..5];
  253.     my($delta,$d,$err)=();
  254.     my($th,$tm,$ts)=&ParseTime($hr,$min,$sec);
  255.     if (defined $hr and $hr ne "") {
  256.       ($hr,$min,$sec)=($th,$tm,$ts);
  257.       $delta="-0:0:1:0:0:0";
  258.     } elsif (defined $min and $min ne "") {
  259.       ($hr,$min,$sec)=($h,$tm,$ts);
  260.       $delta="-0:0:0:1:0:0";
  261.     } elsif (defined $sec and $sec ne "") {
  262.       ($hr,$min,$sec)=($h,$mn,$ts);
  263.       $delta="-0:0:0:0:1:0";
  264.     } else {
  265.       confess "ERROR: invalid arguments in Date_GetPrev.\n";
  266.     }
  267.  
  268.     $d=&Date_SetTime($date,$hr,$min,$sec);
  269.     if ($today) {
  270.       $d=&DateCalc_DateDelta($d,$delta,\$err,0)  if ($d gt $date);
  271.     } else {
  272.       $d=&DateCalc_DateDelta($d,$delta,\$err,0)  if ($d ge $date);
  273.     }
  274.     $date=$d;
  275.   }
  276.   return $date;
  277. }
  278.  
  279. sub Date_GetNext {
  280.   my($date,$day,$today,$hr,$min,$sec)=@_;
  281.   &Date_Init()  if (! $Date::Manip::InitDone);
  282.   my($y,$m,$d,$h,$mn,$s,$err)=();
  283.  
  284.   if (! &CheckDate($date)) {
  285.     $date=&ParseDate($date);
  286.     return ""  if (! $date);
  287.   }
  288.   ($y,$m,$d)=( &CheckDate($date) )[0..2];
  289.  
  290.   if (defined $day and $day ne "") {
  291.     my($day_w)=();
  292.     my($date_w)=&Date_DayOfWeek($m,$d,$y);
  293.     my(%days)=%Date::Manip::Week;
  294.     if (&IsInt($day)) {
  295.       $day_w=$day;
  296.     } else {
  297.       return ""  if (! exists $days{lc($day)});
  298.       $day_w=$days{lc($day)};
  299.     }
  300.     if ($day_w == $date_w) {
  301.       $date=&DateCalc_DateDelta($date,"+0:0:7:0:0:0",\$err,0)  if (! $today);
  302.     } else {
  303.       $date_w -= 7  if ($date_w>$day_w); # make sure next date is greater
  304.       $day = $day_w - $date_w;
  305.       $date=&DateCalc_DateDelta($date,"+0:0:$day:0:0:0",\$err,0);
  306.     }
  307.     $date=&Date_SetTime($date,$hr,$min,$sec)  if (defined $hr);
  308.  
  309.   } else {
  310.     ($h,$mn,$s)=( &CheckDate($date) )[3..5];
  311.     my($delta,$d,$err)=();
  312.     my($th,$tm,$ts)=&ParseTime($hr,$min,$sec);
  313.     if (defined $hr and $hr ne "") {
  314.       ($hr,$min,$sec)=($th,$tm,$ts);
  315.       $delta="+0:0:1:0:0:0";
  316.     } elsif (defined $min and $min ne "") {
  317.       ($hr,$min,$sec)=($h,$tm,$ts);
  318.       $delta="+0:0:0:1:0:0";
  319.     } elsif (defined $sec and $sec ne "") {
  320.       ($hr,$min,$sec)=($h,$mn,$ts);
  321.       $delta="+0:0:0:0:1:0";
  322.     } else {
  323.       confess "ERROR: invalid arguments in Date_GetNext.\n";
  324.     }
  325.  
  326.     $d=&Date_SetTime($date,$hr,$min,$sec);
  327.     if ($today) {
  328.       $d=&DateCalc_DateDelta($d,$delta,\$err,0)  if ($d lt $date);
  329.     } else {
  330.       $d=&DateCalc_DateDelta($d,$delta,\$err,0)  if ($d le $date);
  331.     }
  332.     $date=$d;
  333.   }
  334.  
  335.   return $date;
  336. }
  337.  
  338. sub DateCalc {
  339.   my($D1,$D2,$errref,$mode)=@_;
  340.   my(@date,@delta,$ret,$tmp)=();
  341.  
  342.   if (defined $mode  and  $mode>=0  and  $mode<=2) {
  343.     $Date::Manip::Mode=$mode;
  344.   } else {
  345.     $Date::Manip::Mode=0;
  346.   }
  347.  
  348.   if ($tmp=&ParseDate($D1)) {
  349.     push(@date,$tmp);
  350.   } elsif ($tmp=&ParseDateDelta($D1)) {
  351.     push(@delta,$tmp);
  352.   } else {
  353.     $$errref=1;
  354.     return;
  355.   }
  356.  
  357.   if ($tmp=&ParseDate($D2)) {
  358.     push(@date,$tmp);
  359.   } elsif ($tmp=&ParseDateDelta($D2)) {
  360.     push(@delta,$tmp);
  361.   } else {
  362.     $$errref=2;
  363.     return;
  364.   }
  365.   $mode=$Date::Manip::Mode;
  366.  
  367.   if ($#date==1) {
  368.     $ret=&DateCalc_DateDate(@date,$mode);
  369.   } elsif ($#date==0) {
  370.     $ret=&DateCalc_DateDelta(@date,@delta,$errref,$mode);
  371.   } else {
  372.     $ret=&DateCalc_DeltaDelta(@delta,$mode);
  373.   }
  374.   $ret;
  375. }
  376.  
  377. sub ParseDateDelta {
  378.   my($args,@args,@a,$ref,$date)=();
  379.   local($_)=();
  380.   @a=@_;
  381.  
  382.  
  383.   if ($#a!=0) {
  384.     print "ERROR:  Invalid number of arguments to ParseDateDelta.\n";
  385.     return "";
  386.   }
  387.   $args=$a[0];
  388.   $ref=ref $args;
  389.   if (! $ref) {
  390.     @args=($args);
  391.   } elsif ($ref eq "ARRAY") {
  392.     @args=@$args;
  393.   } elsif ($ref eq "SCALAR") {
  394.     @args=($$args);
  395.   } else {
  396.     print "ERROR:  Invalid arguments to ParseDateDelta.\n";
  397.     return "";
  398.   }
  399.   @a=@args;
  400.  
  401.  
  402.   my($y,$m,$w,$d,$h,$mn,$s,$ys,$ms,$ws,$ds,$hs,$mns,$ss,$dir)=();
  403.   my($def,@delta1,@delta2,$colon,$sign,$delta,$i,$sign)=();
  404.   my($from,$to)=();
  405.   my($workweek)=$Date::Manip::WorkWeekEnd-$Date::Manip::WorkWeekBeg+1;
  406.  
  407.   &Date_Init();
  408.   my($signexp)='([+-]?)';
  409.   my($numexp)='(\d+)';
  410.   my($exp1)="(?: \\s* $signexp \\s* $numexp \\s*)";
  411.   my($yexp,$mexp,$wexp,$dexp,$hexp,$mnexp,$sexp)=();
  412.   $yexp =$mexp=$wexp=$dexp=$hexp=$mnexp=$sexp="()()";
  413.   $yexp ="(?: $exp1 $Date::Manip::YExp)?"  if ($Date::Manip::YExp);
  414.   $mexp ="(?: $exp1 $Date::Manip::MExp)?"  if ($Date::Manip::MExp);
  415.   $wexp ="(?: $exp1 $Date::Manip::WExp)?"  if ($Date::Manip::WExp);
  416.   $dexp ="(?: $exp1 $Date::Manip::DExp)?"  if ($Date::Manip::DExp);
  417.   $hexp ="(?: $exp1 $Date::Manip::HExp)?"  if ($Date::Manip::HExp);
  418.   $mnexp="(?: $exp1 $Date::Manip::MnExp)?" if ($Date::Manip::MnExp);
  419.   $sexp ="(?: $exp1 $Date::Manip::SExp)?"  if ($Date::Manip::SExp);
  420.   my($future)=$Date::Manip::Future;
  421.   my($past)=$Date::Manip::Past;
  422.  
  423.   $delta="";
  424.   PARSE: while (@a) {
  425.     $_ = join(" ",@a);
  426.     s/\s*$//;
  427.  
  428.     if      ($Date::Manip::Exact and s/$Date::Manip::Exact//) {
  429.       $Date::Manip::Mode=0;
  430.     } elsif ($Date::Manip::Approx and s/$Date::Manip::Approx//) {
  431.       $Date::Manip::Mode=1;
  432.     } elsif ($Date::Manip::Business and s/$Date::Manip::Business//) {
  433.       $Date::Manip::Mode=2;
  434.     } elsif (! defined $Date::Manip::Mode) {
  435.       $Date::Manip::Mode=0;
  436.     }
  437.     $workweek=7  if ($Date::Manip::Mode != 2);
  438.  
  439.     foreach $from (keys %Date::Manip::Replace) {
  440.       $to=$Date::Manip::Replace{$from};
  441.       s/(^|[^a-z])$from($|[^a-z])/$1$to$2/i;
  442.     }
  443.  
  444.     s/^\s* $future \s*//ix  if ($future ne "(?:)");
  445.     $dir=1;
  446.     $dir=-1  if ($past  and  s/\s*$past\s*//i);
  447.  
  448.     $colon="";
  449.     if (s/$signexp?$numexp?(:($signexp?$numexp)?)+$//) {
  450.       $colon=$&;
  451.       s/\s*$//;
  452.     }
  453.  
  454.     $sign="+";
  455.     s/^$yexp $mexp $wexp $dexp $hexp $mnexp $sexp$//xi;
  456.     ($ys,$y,$ms,$m,$ws,$w,$ds,$d,$hs,$h,$mns,$mn,$ss,$s)=
  457.       ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14);
  458.     (defined($ys)  and $ys)  ? ($sign=$ys)  : ($ys=$sign);
  459.     (defined($ms)  and $ms)  ? ($sign=$ms)  : ($ms=$ys);
  460.     (defined($ws)  and $ws)  ? ($sign=$ws)  : ($ws=$ms);
  461.     (defined($ds)  and $ds)  ? ($sign=$ds)  : ($ds=$ws);
  462.     (defined($hs)  and $hs)  ? ($sign=$hs)  : ($hs=$ds);
  463.     (defined($mns) and $mns) ? ($sign=$mns) : ($mns=$hs);
  464.     (defined($ss)  and $ss)  ? ($sign=$ss)  : ($ss=$ms);
  465.  
  466.     $def=-1;
  467.     (defined($y))  ? ($def=0) : ($y=0);
  468.     (defined($m))  ? ($def=1) : ($m=0);
  469.     (defined($d))  ? ($def=2) : ($d=0);
  470.     (defined($h))  ? ($def=3) : ($h=0);
  471.     (defined($mn)) ? ($def=4) : ($mn=0);
  472.     (defined($s))  ? ($def=5) : ($s=0);
  473.  
  474.     if (defined $w  and  $w) {
  475.       $d= "$ds$d" + $workweek*"$ws$w";
  476.       if ($d<0) {
  477.         $ds="-";
  478.         $d=-$d;
  479.       }
  480.     }
  481.  
  482.     @delta1=("$ys$y","$ms$m","$ds$d","$hs$h","$mns$mn","$ss$s");
  483.  
  484.     @delta2=&CheckDelta($colon,$sign);
  485.  
  486.     if ($_ or ($def+$#delta2)>4) {
  487.       pop(@a);
  488.       next PARSE;
  489.     }
  490.  
  491.     unshift (@delta2,"+0")  while ($#delta2<5);
  492.     for ($i=0; $i<=5; $i++) {
  493.       $delta1[$i] += $delta2[$i];
  494.       $delta1[$i] *= $dir  if ($delta1[$i] != 0);
  495.       $delta1[$i] = "+".$delta1[$i]  if ($delta1[$i]>0);
  496.     }
  497.  
  498.     $delta=join(":",@delta1);
  499.     splice(@args,0,$#a+1);
  500.     @$args=@args  if (defined $ref  and  $ref eq "ARRAY");
  501.     last PARSE;
  502.   }
  503.  
  504.   $delta=&NormalizeDelta($delta,$Date::Manip::Mode);
  505.   return $delta;
  506. }
  507.  
  508. sub UnixDate {
  509.   my($date,@format)=@_;
  510.   local($_)=();
  511.   my($format,%f,$out,@out,$c,$date1,$date2)=();
  512.   my($scalar)=();
  513.   $date=&ParseDate($date);
  514.   return  if (! $date);
  515.  
  516.   ($f{"Y"},$f{"m"},$f{"d"},$f{"H"},$f{"M"},$f{"S"})=&CheckDate($date);
  517.   $f{"y"}=substr $f{"Y"},2;
  518.   my($m,$d,$y)=($f{"m"},$f{"d"},$f{"Y"});
  519.   &Date_Init();
  520.  
  521.   if (! wantarray) {
  522.     $format=join(" ",@format);
  523.     @format=($format);
  524.     $scalar=1;
  525.   }
  526.  
  527.   $_=$m;
  528.   s/^0//;
  529.   $f{"b"}=$f{"h"}=$Date::Manip::Mon[$_-1];
  530.   $f{"B"}=$Date::Manip::Month[$_-1];
  531.   $_=$m;
  532.   s/^0/ /;
  533.   $f{"f"}=$_;
  534.   $f{"U"}=&Date_WeekOfYear($m,$d,$y,0);
  535.   $f{"W"}=&Date_WeekOfYear($m,$d,$y,1);
  536.   $f{"U"}="0".$f{"U"}  if (length $f{"U"} < 2);
  537.   $f{"W"}="0".$f{"W"}  if (length $f{"W"} < 2);
  538.  
  539.   $f{"j"}=&Date_DayOfYear($m,$d,$y);
  540.   $_=$d;
  541.   s/^0/ /;
  542.   $f{"e"}=$_;
  543.   $f{"w"}=&Date_DayOfWeek($m,$d,$y);
  544.   $f{"v"}=$Date::Manip::W[$f{"w"}];
  545.   $f{"v"}=" ".$f{"v"}  if (length $f{"v"} < 2);
  546.   $f{"a"}=$Date::Manip::Wk[$f{"w"}];
  547.   $f{"A"}=$Date::Manip::Week[$f{"w"}];
  548.   $f{"E"}=&Date_DaySuffix($f{"e"});
  549.  
  550.   $_=$f{"H"};
  551.   s/^0/ /;
  552.   $f{"k"}=$_;
  553.   $f{"i"}=$f{"k"}+1;
  554.   $f{"i"}=$f{"k"};
  555.   $f{"i"}=12          if ($f{"k"}==0);
  556.   $f{"i"}=$f{"k"}-12  if ($f{"k"}>12);
  557.   $f{"i"}=$f{"i"}-12  if ($f{"i"}>12);
  558.   $f{"i"}=" ".$f{"i"} if (length($f{"i"})<2);
  559.   $f{"I"}=$f{"i"};
  560.   $f{"I"}=~ s/^ /0/;
  561.   $f{"p"}=$Date::Manip::Am;
  562.   $f{"p"}=$Date::Manip::Pm  if ($f{"k"}>11);
  563.  
  564.   $f{"o"}=&Date_SecsSince1970($m,$d,$y,$f{"H"},$f{"M"},$f{"S"});
  565.   $f{"s"}=&Date_SecsSince1970GMT($m,$d,$y,$f{"H"},$f{"M"},$f{"S"});
  566.   $f{"z"}=$f{"Z"}=
  567.     ($Date::Manip::ConvTZ eq "IGNORE" or $Date::Manip::ConvTZ eq "" ?
  568.      $Date::Manip::TZ : $Date::Manip::ConvTZ);
  569.  
  570.   $f{"c"}=qq|$f{"a"} $f{"b"} $f{"e"} $f{"H"}:$f{"M"}:$f{"S"} $y|;
  571.   $f{"C"}=$f{"u"}=
  572.     qq|$f{"a"} $f{"b"} $f{"e"} $f{"H"}:$f{"M"}:$f{"S"} $f{"z"} $y|;
  573.   $f{"g"}=qq|$f{"a"},$d $f{"b"} $y $f{"H"}:$f{"M"}:$f{"S"} $f{"z"}|;
  574.   $f{"D"}=$f{"x"}=qq|$m/$d/$f{"y"}|;
  575.   $f{"r"}=qq|$f{"I"}:$f{"M"}:$f{"S"} $f{"p"}|;
  576.   $f{"R"}=qq|$f{"H"}:$f{"M"}|;
  577.   $f{"T"}=$f{"X"}=qq|$f{"H"}:$f{"M"}:$f{"S"}|;
  578.   $f{"V"}=qq|$m$d$f{"H"}$f{"M"}$f{"y"}|;
  579.   $f{"Q"}="$y$m$d";
  580.   $f{"q"}=qq|$y$m$d$f{"H"}$f{"M"}$f{"S"}|;
  581.   $f{"P"}=qq|$y$m$d$f{"H"}:$f{"M"}:$f{"S"}|;
  582.   $f{"F"}=qq|$f{"A"}, $f{"B"} $f{"e"}, $f{"Y"}|;
  583.  
  584.   $f{"n"}="\n";
  585.   $f{"t"}="\t";
  586.   $f{"%"}="%";
  587.   $f{"+"}="+";
  588.  
  589.   foreach $format (@format) {
  590.     $format=reverse($format);
  591.     $out="";
  592.     while ($format) {
  593.       $c=chop($format);
  594.       if ($c eq "%") {
  595.         $c=chop($format);
  596.         if ($c eq "l") {
  597.           $date1=&DateCalc_DateDelta($Date::Manip::Curr,"-0:6:0:0:0:0");
  598.           $date2=&DateCalc_DateDelta($Date::Manip::Curr,"+0:6:0:0:0:0");
  599.           if ($date gt $date1  and  $date lt $date2) {
  600.             $f{"l"}=qq|$f{"b"} $f{"e"} $f{"H"}:$f{"M"}|;
  601.           } else {
  602.             $f{"l"}=qq|$f{"b"} $f{"e"}  $f{"Y"}|;
  603.           }
  604.           $out .= $f{"$c"};
  605.         } elsif (exists $f{"$c"}) {
  606.           $out .= $f{"$c"};
  607.         } else {
  608.           $out .= $c;
  609.         }
  610.       } else {
  611.         $out .= $c;
  612.       }
  613.     }
  614.     push(@out,$out);
  615.   }
  616.   if ($scalar) {
  617.     return $out[0];
  618.   } else {
  619.     return (@out);
  620.   }
  621. }
  622.  
  623. sub ParseDate {
  624.   my($args,@args,@a,$ref,$date)=();
  625.   local($_)=();
  626.   @a=@_;
  627.   my($y,$m,$d,$h,$mn,$s,$i,$which,$dofw,$wk,$tmp,$z,$num,$err)=();
  628.  
  629.  
  630.   if ($#a!=0) {
  631.     print "ERROR:  Invalid number of arguments to ParseDate.\n";
  632.     return "";
  633.   }
  634.   $args=$a[0];
  635.   $ref=ref $args;
  636.   if (! $ref) {
  637.     return $args  if (&CheckDate($args));
  638.     @args=($args);
  639.   } elsif ($ref eq "ARRAY") {
  640.     @args=@$args;
  641.   } elsif ($ref eq "SCALAR") {
  642.     return $$args  if (&CheckDate($$args));
  643.     @args=($$args);
  644.   } else {
  645.     print "ERROR:  Invalid arguments to ParseDate.\n";
  646.     return "";
  647.   }
  648.   @a=@args;
  649.  
  650.  
  651.   &Date_Init();
  652.   my($type)=$Date::Manip::DateFormat;
  653.   my($mmm)=$Date::Manip::MonExp;
  654.   my($now)=$Date::Manip::Now;
  655.   my($offset)=$Date::Manip::Offset;
  656.   my($wkexp)=$Date::Manip::WkExp;
  657.   my($timeexp)=$Date::Manip::TimesExp;
  658.   my(%dofw)=%Date::Manip::Week;
  659.   my($whichexp)=$Date::Manip::WhichExp;
  660.   my(%which)=%Date::Manip::Which;
  661.   my($daysexp)=$Date::Manip::DayExp;
  662.   my(%dayshash)=%Date::Manip::Day;
  663.   my($ampm)=$Date::Manip::AmPmExp;
  664.   my($weeks)=$Date::Manip::WExp;
  665.   my($next)=$Date::Manip::Next;
  666.   my($prev)=$Date::Manip::Prev;
  667.   my($ago)=$Date::Manip::Past;
  668.  
  669.   my($hm)=$Date::Manip::SepHM;
  670.   my($ms)=$Date::Manip::SepMS;
  671.   my($ss)=$Date::Manip::SepSS;
  672.   my($YY) ='(\d{2}|\d{4})'; # 2 or 4 digits (year)
  673.   my($DD)='(\d{2})';        # 2 digits      (month/day/hour/minute/second)
  674.   my($D) ='(\d{1,2})';      # 1 or 2 digit  (month/day/hour)
  675.   my($FD)="(?:$ss\\d+)?";   # fractional secs
  676.   my($zone)=$Date::Manip::ZoneExp;
  677.   my($Time) ="(?:$DD$hm$DD(?:$ms$DD$FD)?(?:\\s*$ampm)?$zone)";
  678.   my($time) ="(?:$D$hm$DD(?:$ms$DD$FD)?(?:\\s*$ampm)?$zone)";
  679.   my($sep)='([\/ .-])';
  680.   my($at)='\s*'.$Date::Manip::At;
  681.   my($in)='\s*'.$Date::Manip::In;
  682.   my($on)='\s*'.$Date::Manip::On;
  683.   my($com)=',?';
  684.  
  685.   $ampm="";
  686.  
  687.   $date="";
  688.   PARSE: while($#a>=0) {
  689.     $_=join(" ",@a);
  690.  
  691.     if      ($Date::Manip::Exact and s/$Date::Manip::Exact//) {
  692.       $Date::Manip::Mode=0;
  693.     } elsif ($Date::Manip::Approx and s/$Date::Manip::Approx//) {
  694.       $Date::Manip::Mode=1;
  695.     } elsif ($Date::Manip::Business and s/$Date::Manip::Business//) {
  696.       $Date::Manip::Mode=2;
  697.     } elsif (! defined $Date::Manip::Mode) {
  698.       $Date::Manip::Mode=0;
  699.     }
  700.  
  701.     if ($timeexp ne "()"  and  /$timeexp/i) {
  702.       $tmp=$1;
  703.       $tmp=$Date::Manip::Times{$tmp};
  704.       s/$timeexp/ $tmp /;
  705.     }
  706.  
  707.     $tmp=0;
  708.     if (/^\s*$whichexp\s*$wkexp$in$mmm\s*$YY?(?:$at$time)?\s*$/i) {
  709.       ($which,$dofw,$m,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8,$9);
  710.       &Date_ErrorCheck(\$y,\$m,\$d,\$h,\$mn,\$s,\$ampm,\$wk);
  711.       $dofw=$dofw{lc($dofw)};
  712.       $which=$which{lc($which)};
  713.       if ($Date::Manip::DateFormat eq "US") {
  714.         $date=&ParseDate("$m 1 $y $h:$mn:$s");
  715.       } else {
  716.         $date=&ParseDate("1 $m $y $h:$mn:$s");
  717.       }
  718.       if ($which==-1) {
  719.         $date=&DateCalc_DateDelta($date,"+0:1:0:0:0:0",\$err,0);
  720.         $date=&Date_GetPrev($date,$dofw,0);
  721.       } else {
  722.         for ($i=0; $i<$which; $i++) {
  723.           if ($i==0) {
  724.             $date=&Date_GetNext($date,$dofw,1);
  725.           } else {
  726.             $date=&Date_GetNext($date,$dofw,0);
  727.           }
  728.           $date="err", last PARSE  if (! $date);
  729.         }
  730.       }
  731.       last PARSE;
  732.  
  733.     } elsif (/^\s*$next\s*$wkexp\s*(?:$at$time)?\s*$/i) {
  734.       ($dofw,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  735.       $date=&Date_GetNext($Date::Manip::Curr,$dofw,0,$h,$mn,$s);
  736.       last PARSE;
  737.  
  738.     } elsif (/^\s*$prev\s*$wkexp\s*(?:$at$time)?\s*$/i) {
  739.       ($dofw,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  740.       $date=&Date_GetPrev($Date::Manip::Curr,$dofw,0,$h,$mn,$s);
  741.       last PARSE;
  742.  
  743.     } elsif (/^\s*$next\s*$weeks\s*(?:$at$time)?\s*$/i) {
  744.       ($h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  745.       $date=&DateCalc_DateDelta($Date::Manip::Curr,"+0:0:7:0:0:0",\$err,0);
  746.       $date=&Date_SetTime($date,$h,$mn,$s)  if (defined $h);
  747.       last PARSE;
  748.     } elsif (/^\s*$prev\s*$weeks\s*(?:$at$time)?\s*$/i) {
  749.       ($h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  750.       $date=&DateCalc_DateDelta($Date::Manip::Curr,"-0:0:7:0:0:0",\$err,0);
  751.       $date=&Date_SetTime($date,$h,$mn,$s)  if (defined $h);
  752.       last PARSE;
  753.  
  754.     } elsif (/^\s*$in\s*(\d+)\s*$weeks\s*(?:$at$time)?\s*$/i) {
  755.       ($num,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  756.       $date=&DateCalc_DateDelta($Date::Manip::Curr,"+0:0:" .7*$num. ":0:0:0",
  757.                                 \$err,0);
  758.       $date=&Date_SetTime($date,$h,$mn,$s)  if (defined $h);
  759.       last PARSE;
  760.     } elsif (/^\s*(\d+)\s*$weeks\s*$ago\s*(?:$at$time)?\s*$/i) {
  761.       ($num,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  762.       $date=&DateCalc_DateDelta($Date::Manip::Curr,"-0:0:" .7*$num. ":0:0:0",
  763.                                \$err,0);
  764.       $date=&Date_SetTime($date,$h,$mn,$s)  if (defined $h);
  765.       last PARSE;
  766.  
  767.     } elsif (/^\s*$wkexp\s*$in\s*(\d+)\s*$weeks(?:$at$time)?\s*$/i) {
  768.       ($dofw,$num,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7);
  769.       $tmp="+";
  770.     } elsif (/^\s*$wkexp\s*(\d+)\s*$weeks\s*$ago(?:$at$time)?\s*$/i) {
  771.       ($dofw,$num,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7);
  772.       $tmp="-";
  773.     } elsif (/^\s*$in\s*(\d+)\s*$weeks$on$wkexp(?:$at$time)?\s*$/i) {
  774.       ($num,$dofw,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7);
  775.       $tmp="+"
  776.     } elsif (/^\s*(\d+)\s*$weeks\s*$ago$on$wkexp(?:$at$time)?\s*$/i) {
  777.       ($num,$dofw,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7);
  778.       $tmp="-";
  779.     }
  780.     if ($tmp) {
  781.       $date=&DateCalc_DateDelta($Date::Manip::Curr,
  782.                                 $tmp . "0:0:" .7*$num. ":0:0:0",\$err,0);
  783.       $date=&Date_GetPrev($date,$Date::Manip::FirstDay,1);
  784.       $date=&Date_GetNext($date,$dofw,1,$h,$mn,$s);
  785.       last PARSE;
  786.     }
  787.  
  788.     if (/$daysexp/i) {
  789.       $tmp=lc($1);
  790.       $tmp=$dayshash{"$tmp"};
  791.       s/\s*$daysexp\s*/ $tmp /;
  792.       s/^\s+//;
  793.       s/\s+$//;
  794.     }
  795.  
  796.     $tmp=0;
  797.     if (/^\s*$D\s*$wkexp\s*(?:$in?\s*$YY)?(?:\s*$at?\s*$time)?\s*$/i) {
  798.       ($which,$dofw,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  799.       $tmp=1;
  800.     } elsif (/^\s*$wkexp\s*$weeks\s*$D(?:$in?\s*$YY)?(?:\s*$at?\s*$time)?\s*$/i) {
  801.       ($dofw,$which,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  802.       $tmp=1;
  803.     } elsif (/^\s*$wkexp\s*$D\s*$weeks(?:$in?\s*$YY)?(?:\s*$at?\s*$time)?\s*$/i) {
  804.       ($dofw,$which,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  805.       $tmp=1;
  806.     }
  807.     if ($tmp) {
  808.       $y=""  if (! defined $y);
  809.       $date=&ParseDate("1 1 $y");
  810.       $date=&Date_GetNext($date,$dofw,1);
  811.       $date=&DateCalc_DateDelta($date,"+0:0:". ($which-1)*7 . ":0:0:0",\$err,0)
  812.         if ($which>1);
  813.       $date=&Date_SetTime($date,$h,$mn,$s)  if (defined $h);
  814.       last PARSE;
  815.     }
  816.  
  817.     if ($wkexp ne "()" and /$wkexp/i) {
  818.       $wk=$1;
  819.       s/$wkexp$com/ /i;
  820.     }
  821.     s/\s+/ /g;                  # all whitespace are now a single space
  822.     s/^\s+//;
  823.     s/\s+$//;
  824.  
  825.     if (/^$YY$DD$DD$at?$Time?$/i) {
  826.       ($y,$m,$d,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  827.  
  828.     } elsif (/^(\d{4})$DD$DD$DD$DD$DD$/i) {
  829.       ($y,$m,$d,$h,$mn,$s)=($1,$2,$3,$4,$5,$6);
  830.  
  831.     } elsif (/^$D$sep$D(?:\2$YY)?(?:(?:$com\s+|$com$at|\2)$time)?$/i) {
  832.       ($m,$d,$y,$h,$mn,$s,$ampm,$z)=($1,$3,$4,$5,$6,$7,$8,$9);
  833.       ($m,$d)=($d,$m)  if ($type ne "US");
  834.  
  835.     } elsif (/^$mmm$sep$D(?:(?:$com\s*|\2)$YY)?$com(?:(?:\s+|$at)$time)?$/i) {
  836.       ($m,$d,$y,$h,$mn,$s,$ampm,$z)=($1,$3,$4,$5,$6,$7,$8,$9);
  837.  
  838.     } elsif (/^$mmm$sep$D(?:\2$YY)?(?:\2$time)?$/i) {
  839.       ($m,$d,$y,$h,$mn,$s,$ampm,$z)=($1,$3,$4,$5,$6,$7,$8,$9);
  840.  
  841.     } elsif (/^$D$sep*$mmm(?:(?:$com\s*|\2)$YY)?$com(?:(?:\s+|$at)$time)?$/i) {
  842.       ($d,$m,$y,$h,$mn,$s,$ampm,$z)=($1,$3,$4,$5,$6,$7,$8,$9);
  843.  
  844.     } elsif (/^$D$sep*$mmm(?:\2$YY)?(?:\2$time)?$/i) {
  845.       ($d,$m,$y,$h,$mn,$s,$ampm,$z)=($1,$3,$4,$5,$6,$7,$8,$9);
  846.  
  847.     } elsif (/^$D\s*$mmm(?:$com\s*$YY)?$com(?:(?:\s+|$at)$time)?$/i) {
  848.       ($d,$m,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  849.  
  850.     } elsif (/^$mmm$D(?:$com\s*$YY)?$com(?:(?:\s+|$at)$time)?$/i) {
  851.       ($m,$d,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  852.  
  853.     } elsif (/^$mmm\s*$DD(?:$com$YY)?$com(?:(?:\s+|$at)$time)?$/i) {
  854.       ($m,$d,$y,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6,$7,$8);
  855.  
  856.  
  857.     } elsif (/^$time$/) {
  858.       ($h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5);
  859.  
  860.     } elsif (/^$time\s*$D$sep$D(?:\7$YY)?$/i) {
  861.       ($h,$mn,$s,$ampm,$z,$m,$d,$y)=($1,$2,$3,$4,$5,$6,$8,$9);
  862.       ($m,$d)=($d,$m)  if ($type ne "US");
  863.  
  864.     } elsif (/^$time$sep$D\6$D(?:\6$YY)?$/i) {
  865.       ($h,$mn,$s,$ampm,$z,$m,$d,$y)=($1,$2,$3,$4,$5,$7,$8,$9);
  866.       ($m,$d)=($d,$m)  if ($type ne "US");
  867.  
  868.     } elsif (/^$time\s*$mmm$sep$D(?:(?:$com\s*|\7)$YY)?$/i) {
  869.       ($h,$mn,$s,$ampm,$z,$m,$d,$y)=($1,$2,$3,$4,$5,$6,$8,$9);
  870.  
  871.     } elsif (/^$time$sep$mmm\6$D(?:\6$YY)?$/i) {
  872.       ($h,$mn,$s,$ampm,$z,$m,$d,$y)=($1,$2,$3,$4,$5,$7,$8,$9);
  873.  
  874.     } elsif (/^$time\s*$D$sep$mmm(?:(?:$com\s*|\7)$YY)?$/i) {
  875.       ($h,$mn,$s,$ampm,$z,$d,$m,$y)=($1,$2,$3,$4,$5,$6,$8,$9);
  876.  
  877.     } elsif (/^$time$sep$D\6$mmm(?:\6$YY)?$/i) {
  878.       ($h,$mn,$s,$ampm,$z,$d,$m,$y)=($1,$2,$3,$4,$5,$7,$8,$9);
  879.  
  880.     } elsif (/^$time\s*$mmm\s*$D(?:$com\s+$YY)?$/i) {
  881.       ($h,$mn,$s,$ampm,$z,$m,$d,$y)=($1,$2,$3,$4,$5,$6,$7,$8);
  882.  
  883.     } elsif (/^$time\s*$mmm\s*$DD$com$YY$/i) {
  884.       ($h,$mn,$s,$ampm,$z,$m,$d,$y)=($1,$2,$3,$4,$5,$6,$7,$8);
  885.  
  886.     } elsif (/^$time\s*$D\s*$mmm(?:$com\s*$YY)?$/i) {
  887.       ($h,$mn,$s,$ampm,$z,$d,$m,$y)=($1,$2,$3,$4,$5,$6,$7,$8);
  888.  
  889.     } elsif (/^$now$at?$time?$/i) {
  890.       ($h,$mn,$s,$ampm,$z)=($2,$3,$4,$5,$6);
  891.       $date=$Date::Manip::Curr;
  892.       if (defined $h) {
  893.         if (&Date_ErrorCheck(\$y,\$m,\$d,\$h,\$mn,\$s,\$ampm,\$wk)) {
  894.           pop(@a);
  895.           next PARSE;
  896.         }
  897.         $date=&Date_SetTime($date,$h,$mn,$s);
  898.       }
  899.       $date=&Date_ConvTZ($date,$z);
  900.       return $date;
  901.  
  902.     } elsif (/^$mmm\s*$D\s+$time\s*$YY$/i) {
  903.       ($m,$d,$h,$mn,$s,$ampm,$z,$y)=($1,$2,$3,$4,$5,$6,$7,$8);
  904.  
  905.     } elsif (/^$offset$at?$time?$/i) {
  906.       ($offset,$h,$mn,$s,$ampm,$z)=($1,$2,$3,$4,$5,$6);
  907.       $offset=$Date::Manip::Offset{lc($offset)};
  908.       $date=&DateCalc_DateDelta($Date::Manip::Curr,$offset,\$err,0);
  909.       if (defined $h) {
  910.         if (&Date_ErrorCheck(\$y,\$m,\$d,\$h,\$mn,\$s,\$ampm,\$wk)) {
  911.           pop(@a);
  912.           next PARSE;
  913.         }
  914.         $date=&Date_SetTime($date,$h,$mn,$s);
  915.         $date=&Date_ConvTZ($date,$z);
  916.       }
  917.       return $date;
  918.  
  919.     } else {
  920.       pop(@a);
  921.       next PARSE;
  922.     }
  923.  
  924.     if (&Date_ErrorCheck(\$y,\$m,\$d,\$h,\$mn,\$s,\$ampm,\$wk)) {
  925.       pop(@a);
  926.     } else {
  927.       last PARSE;
  928.     }
  929.   }
  930.  
  931.   if ($date ne "err") {
  932.     if (@a) {
  933.       splice(@args,0,$#a+1);
  934.       @$args=@args  if (defined $ref  and  $ref eq "ARRAY");
  935.       $date=&FormDate($y,$m,$d,$h,$mn,$s)  if (! $date);
  936.       $date=&Date_ConvTZ($date,$z);
  937.       return $date;
  938.     }
  939.   }
  940.   return "";
  941. }
  942.  
  943.  
  944. sub Date_DayOfWeek {
  945.   my($m,$d,$y)=@_;
  946.   confess "ERROR: Date_DayOfWeek requires a 4 digit year.\n"
  947.     if ($y!~/^\d{4}$/);
  948.   my($dayofweek,$dec31)=();
  949.  
  950.   $dec31=2;                     # Dec 31, 0999 was Tuesday
  951.   $dayofweek=(&Date_DaysSince999($m,$d,$y)+$dec31) % 7;
  952.   return $dayofweek;
  953. }
  954.  
  955. sub Date_SecsSince1970 {
  956.   my($m,$d,$y,$h,$mn,$s)=@_;
  957.   confess "ERROR: Date_SecsSince1970 requires a 4 digit year.\n"
  958.     if ($y!~/^\d{4}$/);
  959.   my($sec_now,$sec_70)=();
  960.   $sec_now=(&Date_DaysSince999($m,$d,$y)-1)*24*3600 + $h*3600 + $mn*60 + $s;
  961.   $sec_70 =30610224000;
  962.   return ($sec_now-$sec_70);
  963. }
  964.  
  965. sub Date_SecsSince1970GMT {
  966.   my($m,$d,$y,$h,$mn,$s)=@_;
  967.   confess "ERROR: Date_SecsSince1970GMT requires a 4 digit year.\n"
  968.     if ($y!~/^\d{4}$/);
  969.  
  970.   my($sec)=&Date_SecsSince1970($m,$d,$y,$h,$mn,$s);
  971.   return $sec   if ($Date::Manip::ConvTZ eq "IGNORE");
  972.  
  973.   my($tz)=$Date::Manip::ConvTZ;
  974.   $tz=$Date::Manip::TZ  if (! $tz);
  975.   $tz=$Date::Manip::Zone{lc($tz)}  if ($tz !~ /^[+-]\d{4}$/);
  976.  
  977.   my($tzs)=1;
  978.   $tzs=-1 if ($tz<0);
  979.   $tz=~/.(..)(..)/;
  980.   my($tzh,$tzm)=($1,$2);
  981.   $sec - $tzs*($tzh*3600+$tzm*60);
  982. }
  983.  
  984. sub Date_DaysSince999 {
  985.   my($m,$d,$y)=@_;
  986.   confess "ERROR: Date_DaysSince999 requires a 4 digit year.\n"
  987.     if ($y!~/^\d{4}$/);
  988.   my($Ny,$N4,$N100,$N400,$dayofyear,$days)=();
  989.   my($cc,$yy)=();
  990.  
  991.   $y=~ /(\d{2})(\d{2})/;
  992.   ($cc,$yy)=($1,$2);
  993.  
  994.   $Ny=$y-1000;
  995.  
  996.   $N4=int(($Ny-1)/4)+1;
  997.   $N4=0         if ($y==1000);
  998.  
  999.   $N100=$cc-9;
  1000.   $N100--       if ($yy==0);
  1001.  
  1002.   $N400=int(($N100+1)/4);
  1003.  
  1004.   $dayofyear=&Date_DayOfYear($m,$d,$y);
  1005.   $days= $Ny*365 + $N4 - $N100 + $N400 + $dayofyear;
  1006.  
  1007.   return $days;
  1008. }
  1009.  
  1010. sub Date_DayOfYear {
  1011.   my($m,$d,$y)=@_;
  1012.   confess "ERROR: Date_DayOfYear requires a 4 digit year.\n"
  1013.     if ($y!~/^\d{4}$/);
  1014.   my(@daysinmonth)=(0,31,28,31,30,31,30,31,31,30,31,30,31);
  1015.   my($daynum,$i)=();
  1016.   $daysinmonth[2]=29  if (&Date_LeapYear($y));
  1017.   $daynum=0;
  1018.   for ($i=1; $i<$m; $i++) {
  1019.     $daynum += $daysinmonth[$i];
  1020.   }
  1021.   $daynum += $d;
  1022.   $daynum="0$daynum"   if ($daynum<10);
  1023.   $daynum="0$daynum"   if ($daynum<100);
  1024.   return $daynum;
  1025. }
  1026.  
  1027. sub Date_DaysInYear {
  1028.   my($y)=@_;
  1029.   confess "ERROR: Date_DaysInYear requires a 4 digit year.\n"
  1030.     if ($y!~/^\d{4}$/);
  1031.   return 366  if (&Date_LeapYear($y));
  1032.   return 365;
  1033. }
  1034.  
  1035. sub Date_WeekOfYear {
  1036.   my($m,$d,$y,$f)=@_;
  1037.   confess "ERROR: Date_WeekOfWeek requires a 4 digit year.\n"
  1038.     if ($y!~/^\d{4}$/);
  1039.   my($jan1)=&Date_DayOfWeek(1,1,$y); # Jan 1 is what day of week
  1040.   my($dofy)=&Date_DayOfYear($m,$d,$y);
  1041.  
  1042.   $jan1=$jan1-$f;
  1043.   $jan1+=7 if ($jan1<0);
  1044.  
  1045.   $dofy+=$jan1-1;
  1046.  
  1047.   return (int($dofy/7)+1);
  1048. }
  1049.  
  1050. sub Date_LeapYear {
  1051.   my($y)=@_;
  1052.   confess "ERROR: Date_LeapYear requires a 4 digit year.\n"
  1053.     if ($y!~/^\d{4}$/);
  1054.   return 0 unless $y % 4 == 0;
  1055.   return 1 unless $y % 100 == 0;
  1056.   return 0 unless $y % 400 == 0;
  1057.   return 1;
  1058. }
  1059.  
  1060. sub Date_DaySuffix {
  1061.   my($d)=@_;
  1062.   return $Date::Manip::Day[$d-1];
  1063. }
  1064.  
  1065. sub Date_ConvTZ {
  1066.   my($date,$from,$to)=@_;
  1067.   my($gmt)=();
  1068.  
  1069.   if (! defined $to  or  ! $to) {
  1070.     return $date
  1071.       if (! defined $from  or  ! $from  or  $Date::Manip::ConvTZ eq "IGNORE");
  1072.  
  1073.     $to=$Date::Manip::ConvTZ  if (! defined $to  or  ! $to);
  1074.     $to=$Date::Manip::TZ  if (! $to);
  1075.   }
  1076.   if (! defined $from  or  ! $from) {
  1077.     $from=$Date::Manip::ConvTZ;
  1078.     $from=$Date::Manip::TZ  if (! $from);
  1079.   }
  1080.  
  1081.   $to=$Date::Manip::Zone{lc($to)}
  1082.     if (exists $Date::Manip::Zone{lc($to)});
  1083.   $from=$Date::Manip::Zone{lc($from)} 
  1084.     if (exists $Date::Manip::Zone{lc($from)});
  1085.   $gmt=$Date::Manip::Zone{gmt};
  1086.  
  1087.   return $date  if ($from !~ /^[+-]\d{4}$/ or $to !~ /^[+-]\d{4}$/);
  1088.   return $date  if ($from eq $to);
  1089.  
  1090.   my($h,$m,$err)=();
  1091.   if ($from ne $gmt) {
  1092.     $from=~/([+-]\d{2})(\d{2})/;
  1093.     ($h,$m)=($1,$2);
  1094.     $h=-$h;
  1095.     $date=&DateCalc_DateDelta($date,"+0:0:0:$h:$m:00",\$err,0);
  1096.   }
  1097.   if ($to ne $gmt) {
  1098.     $to=~/([+-]\d{2})(\d{2})/;
  1099.     ($h,$m)=($1,$2);
  1100.     $date=&DateCalc_DateDelta($date,"+0:0:0:$h:$m:00",\$err,0);
  1101.   }
  1102.   return $date;
  1103. }
  1104.  
  1105. sub Date_TimeZone {
  1106.   my($null,$tz,@tz,$std,$dst,$time,$isdst,$tmp)=();
  1107.  
  1108.  
  1109.   push(@tz,$Date::Manip::TZ)  if (defined $Date::Manip::TZ);  # TZ config var
  1110.   push(@tz,$ENV{"TZ"})        if (exists $ENV{"TZ"});         # TZ environ var
  1111.   unless ((defined $^O and
  1112.            $^O =~ /MSWin32/i ||
  1113.            $^O =~ /Windows_95/i ||
  1114.            $^O =~ /Windows_NT/i) or
  1115.           (defined $ENV{OS} and
  1116.            $ENV{OS} =~ /MSWin32/i ||
  1117.            $ENV{OS} =~ /Windows_95/i ||
  1118.            $ENV{OS} =~ /Windows_NT/i)) {
  1119.     $tz = `date`;
  1120.     chomp($tz);
  1121.     $tz=(split(/\s+/,$tz))[4];
  1122.     push(@tz,$tz);
  1123.   }
  1124.   push(@tz,$main::TZ)         if (defined $main::TZ);         # $main::TZ
  1125.   if (-s "/etc/TIMEZONE") {                                   # /etc/TIMEZONE
  1126.     ($null,$tz) = split (/\=/,`grep ^TZ /etc/TIMEZONE`);
  1127.     chomp($tz);
  1128.     $tz=~ s/\s+//g;
  1129.     push(@tz,$tz);
  1130.   }
  1131.  
  1132.   foreach $tz (@tz) {
  1133.     return uc($tz)
  1134.       if (defined $Date::Manip::Zone{lc($tz)} or $tz=~/^[+-]\d{4}/);
  1135.  
  1136.     if ($tz =~ /^$Date::Manip::CurrZoneExp$/i) {
  1137.       $tmp=lc $1;
  1138.       $tz=$Date::Manip::CurrZone{$tmp};
  1139.     }
  1140.  
  1141.     if ($tz =~ /^([a-z]+)\d([a-z]+)\d?$/i) {
  1142.       ($std,$dst)=($1,$2);
  1143.       next  if (! defined $Date::Manip::Zone{lc($std)} or
  1144.                 ! defined $Date::Manip::Zone{lc($dst)});
  1145.       $time = time();
  1146.       ($null,$null,$null,$null,$null,$null,$null,$null,$isdst) =
  1147.         localtime($time);
  1148.       return uc($dst)  if ($isdst);
  1149.       return uc($std);
  1150.     }
  1151.   }
  1152.  
  1153.   confess "ERROR: Date::Manip unable to determine TimeZone.\n";
  1154. }
  1155.  
  1156. sub Date_Init {
  1157.   my($language,$format,$tz,$convtz,@args)=@_;
  1158.   $Date::Manip::InitDone=1;
  1159.   local($_)=();
  1160.   my($internal,$firstday)=();
  1161.   my($var,$val,$file)=();
  1162.  
  1163.   if (defined $language  and  $language) {
  1164.     if ($language=~ /=/) {
  1165.       push(@args,$language);
  1166.     } else {
  1167.       push(@args,"Language=$language");
  1168.     }
  1169.   }
  1170.   if (defined $format  and  $format) {
  1171.     if ($format=~ /=/) {
  1172.       push(@args,$format);
  1173.     } else {
  1174.       push(@args,"DateFormat=$format");
  1175.     }
  1176.   }
  1177.   if (defined $tz  and  $tz) {
  1178.     if ($tz=~ /=/) {
  1179.       push(@args,$tz);
  1180.     } else {
  1181.       push(@args,"TZ=$tz");
  1182.     }
  1183.   }
  1184.   if (defined $convtz  and  $convtz) {
  1185.     if ($convtz=~ /=/) {
  1186.       push(@args,$convtz);
  1187.     } else {
  1188.       push(@args,"ConvTZ=$convtz");
  1189.     }
  1190.   }
  1191.  
  1192.   $Date::Manip::EraseHolidays=0;
  1193.   foreach (@args) {
  1194.     s/\s*$//;
  1195.     s/^\s*//;
  1196.     /^(\S+) \s* = \s* (.+)$/x;
  1197.     ($var,$val)=($1,$2);
  1198.     $Date::Manip::InitFilesRead--,
  1199.     $Date::Manip::PersonalCnf=$val,      next  if ($var eq "PersonalCnf");
  1200.     $Date::Manip::PersonalCnfPath=$val,  next  if ($var eq "PersonalCnfPath");
  1201.   }
  1202.  
  1203.   $Date::Manip::InitFilesRead=1  if ($Date::Manip::IgnoreGlobalCnf);
  1204.   if ($Date::Manip::InitFilesRead<1) {
  1205.     $Date::Manip::InitFilesRead=1;
  1206.     if ($Date::Manip::GlobalCnf) {
  1207.       $file=&ExpandTilde($Date::Manip::GlobalCnf);
  1208.     }
  1209.     &ReadInitFile($file)  if (defined $file  and  $file  and  -r $file  and
  1210.                               -s $file  and  -f $file);
  1211.   }
  1212.   if ($Date::Manip::InitFilesRead<2) {
  1213.     $Date::Manip::InitFilesRead=2;
  1214.     if ($Date::Manip::PersonalCnf  and  $Date::Manip::PersonalCnfPath) {
  1215.       $file=&SearchPath($Date::Manip::PersonalCnf,
  1216.                         $Date::Manip::PersonalCnfPath,"r");
  1217.     }
  1218.     &ReadInitFile($file)  if (defined $file  and  $file  and  -r $file  and
  1219.                               -s $file  and  -f $file);
  1220.   }
  1221.  
  1222.   foreach (@args) {
  1223.     s/\s*$//;
  1224.     s/^\s*//;
  1225.     /^(\S+) \s* = \s* (.+)$/x;
  1226.     ($var,$val)=($1,$2);
  1227.  
  1228.     &Date_SetConfigVariable($var,$val);
  1229.   }
  1230.  
  1231.   confess "ERROR: Unknown FirstDay in Date::Manip.\n"
  1232.     if (! &IsInt($Date::Manip::FirstDay,0,6));
  1233.   confess "ERROR: Unknown WorkWeekBeg in Date::Manip.\n"
  1234.     if (! &IsInt($Date::Manip::WorkWeekBeg,0,6));
  1235.   confess "ERROR: Unknown WorkWeekEnd in Date::Manip.\n"
  1236.     if (! &IsInt($Date::Manip::WorkWeekEnd,0,6));
  1237.   confess "ERROR: Invalid WorkWeek in Date::Manip.\n"
  1238.     if ($Date::Manip::WorkWeekEnd <= $Date::Manip::WorkWeekBeg);
  1239.  
  1240.   my($i,$j,@tmp,@tmp2,@tmp3,$a,$b,$now,$offset,$last,$in,$at,$on,$tmp,%tmp,
  1241.      $mon,$month,@mon,@month,
  1242.      $w,$wk,$week,@w,@wk,@week,$weeks,
  1243.      $days,@days,$am,$pm,
  1244.      $zones,$zonesrfc,@zones,$times,$future,$past,$sephm,$sepms,$sepss,
  1245.      $years,$months,$days,$hours,$minutes,$seconds,$replace,$next,$prev,
  1246.      $approx,$exact,$business)=();
  1247.   my($lang)=$Date::Manip::Language;
  1248.  
  1249.   if (! $Date::Manip::Init) {
  1250.     $Date::Manip::Init=1;
  1251.  
  1252.  
  1253.  
  1254.     if ($lang eq "English") {
  1255.       $month="January February March April May June ".
  1256.         "July August September October November December";
  1257.       $mon="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec";
  1258.  
  1259.       $week="Sunday Monday Tuesday Wednesday Thursday Friday Saturday";
  1260.       $wk="Sun Mon Tue Wed Thu Fri Sat";
  1261.       $w="S M T W Th F Sa";
  1262.  
  1263.       $days[0]="1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th ".
  1264.         "15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th ".
  1265.         "28th 29th 30th 31st";
  1266.       $days[1]="first second third fourth fifth sixth seventh eighth ninth ".
  1267.         "tenth eleventh twelfth thirteenth fourteenth fifteenth sixteenth ".
  1268.         "seventeenth eighteenth nineteenth twentieth twenty-first ".
  1269.         "twenty-second twenty-third twenty-fourth twenty-fifth twenty-sixth ".
  1270.         "twenty-seventh twenty-eighth twenty-ninth thirtieth thirty-first";
  1271.  
  1272.       $last="last";
  1273.       $in="in of";
  1274.       $at="at";
  1275.       $on="on";
  1276.       $future="in";
  1277.       $past="ago";
  1278.       $next="next";
  1279.       $prev="previous last";
  1280.  
  1281.       $am="AM";
  1282.       $pm="PM";
  1283.  
  1284.       $years  ="y yr year yrs years";
  1285.       $months ="mon month months";
  1286.       $weeks  ="w wk wks week weeks";
  1287.       $days   ="d day days";
  1288.       $hours  ="h hr hrs hour hours";
  1289.       $minutes="mn min minute minutes";
  1290.       $seconds="s sec second seconds";
  1291.       $replace="m month";
  1292.  
  1293.       $now="today now";
  1294.       $offset="yesterday -0:0:1:0:0:0 tomorrow +0:0:1:0:0:0";
  1295.       $times="noon 12:00:00 midnight 00:00:00";
  1296.  
  1297.       $sephm=':';
  1298.       $sepms=':';
  1299.       $sepss='[.:]';
  1300.       $zones="";
  1301.  
  1302.       $exact="exactly";
  1303.       $approx="approximately";
  1304.       $business="business";
  1305.  
  1306.     } elsif ($lang eq "Swedish") {
  1307.       $month="Januari Februari Mars April Maj Juni ".
  1308.         "Juli Augusti September Oktober November December";
  1309.       $mon="Jan Feb Mar Apr Maj Jun Jul Aug Sep Okt Nov Dec";
  1310.  
  1311.       $week="Sundag Mondag Tisdag Onsdag Torsdag Fredag Lurdag";
  1312.       $wk="Sun Mon Tis Ons Tor Fre Lur";
  1313.       $w="S M Ti O To F Lu";
  1314.  
  1315.       $days[0]="1:a 2:a 3:e 4:e 5:e 6:e 7:e 8:e 9:e 10:e 11:e 12:e 13:e 14:e ".
  1316.         "15:e 16:e 17:e 18:e 19:e 20:e 21:a 22:a 23:e 24:e 25:e 26:e 27:e ".
  1317.         "28:e 29:e 30:e 31:a";
  1318.       $days[1]="fursta andra tredje fj=E4rde femte sj=E4tte sjunde ottonde ".
  1319.         "nionde tionde elte tolfte trettonde fjortonde femtonde sextonde ".
  1320.         "sjuttonde artonde nittonde tjugonde tjugofursta ".
  1321.         "tjugoandra tjugotredje tjugofj=E4rde tjugofemte tjugosj=E4tte ".
  1322.         "tjugosjunde tjugoottonde tjugonionde trettionde trettiofursta";
  1323.  
  1324.       $last="furra senaste";
  1325.       $in="om";
  1326.       $on="";
  1327.       $at="kl kl. klockan";
  1328.       $future="";
  1329.       $past="";
  1330.       $next="";
  1331.       $prev="";
  1332.  
  1333.       $am="FM";
  1334.       $pm="EM";
  1335.  
  1336.       $years  ="o or";
  1337.       $months ="mon monad monader";
  1338.       $weeks  ="";
  1339.       $days   ="d dag dagar";
  1340.       $hours  ="t tim timme timmar";
  1341.       $minutes="mn min minut minuter";
  1342.       $seconds="s sek sekund sekunder";
  1343.       $replace="m monad";
  1344.  
  1345.       $now="idag nu";
  1346.       $offset="igor -0:0:1:0:0:0 imorgon +0:0:1:0:0:0";
  1347.       $times="";
  1348.       $sephm='[:.]';
  1349.       $sepms=':';
  1350.       $sepss='[.:]';
  1351.       $zones="";
  1352.  
  1353.       $exact="";
  1354.       $approx="";
  1355.       $business="";
  1356.  
  1357.     } elsif ($lang eq "French") {
  1358.       $month="janvier fevrier mars avril mai juin juillet aout ".
  1359.         "septembre octobre novembre decembre";
  1360.       $mon="jan fev mar avr mai juin juil aou sep oct nov dec";
  1361.  
  1362.       $week="dimanche lundi mardi mercredi jeudi vendredi samedi";
  1363.       $wk="dim lun mar mer jeu ven sam";
  1364.       $w="d l ma me j v s";
  1365.  
  1366.       @tmp=map { ($_."e"); } (1...31);
  1367.       $tmp[0] = "1er";
  1368.       $days[0]=join " ",@tmp;   # 1er 2e 3e ...
  1369.       $days[1]="1re";           # 1re
  1370.       $days[2]="premier deux trois quatre cinq six sept huit neuf dix onze ".
  1371.         "douze treize quatorze quinze size dix-sept dix-huit dix-neuf vingt ".
  1372.         "vingt_et_un vingt-deux vingt-trois vingt-quatre vingt-cinq ".
  1373.         "vingt-six vingt-sept vingt-huit vingt-neuf trente trente_et_un";
  1374.  
  1375.       $last="dernier";
  1376.       $in="en de";
  1377.       $on="";
  1378.       $at="a";
  1379.       $future="en";
  1380.       $past="il_y_a";
  1381.       $next="";
  1382.       $prev="";
  1383.  
  1384.       $am="du_matin";
  1385.       $pm="du_soir";
  1386.  
  1387.       $years  ="an annee ans annees";
  1388.       $months ="mois";
  1389.       $weeks  ="";
  1390.       $days   ="j jour jours";
  1391.       $hours  ="h heure heures";
  1392.       $minutes="mn min minute minutes";
  1393.       $seconds="s sec seconde secondes";
  1394.       $replace="m mois";
  1395.  
  1396.       $now="aujourd'hui maintenant";
  1397.       $offset="hier -0:0:1:0:0:0 demain +0:0:1:0:0:0";
  1398.       $times="";
  1399.       $sephm='[:h]';
  1400.       $sepms=':';
  1401.       $sepss='[.:,]';
  1402.  
  1403.       $zones="";
  1404.  
  1405.       $exact="";
  1406.       $approx="";
  1407.       $business="";
  1408.  
  1409.  
  1410.  
  1411.  
  1412.     } else {
  1413.       confess "ERROR: Unknown language in Date::Manip.\n";
  1414.     }
  1415.  
  1416.     $Date::Manip::MonExp=&Date_Regexp("$mon $month","lc,under,sort,back");
  1417.     ($tmp,@Date::Manip::Mon)  =&Date_Regexp($mon,"under",1);
  1418.     ($tmp,@Date::Manip::Month)=&Date_Regexp($month,"under",1);
  1419.     ($tmp,@tmp2)  =&Date_Regexp($mon,"","val1");
  1420.     ($tmp,@tmp3)=&Date_Regexp($month,"","val1");
  1421.     @tmp=reverse(@tmp2,@tmp3);
  1422.     ($tmp,%Date::Manip::Month)=&Date_Regexp(\@tmp,"lc,under","1");
  1423.  
  1424.     $Date::Manip::WkExp=&Date_Regexp("$wk $week","lc,under,sort,back");
  1425.     ($tmp,@Date::Manip::W)   =&Date_Regexp($w,"",1);
  1426.     ($tmp,@Date::Manip::Wk)  =&Date_Regexp($wk,"",1);
  1427.     ($tmp,@Date::Manip::Week)=&Date_Regexp($week,"",1);
  1428.     ($tmp,@tmp )=&Date_Regexp($w,"","val0");
  1429.     ($tmp,@tmp2)=&Date_Regexp($wk,"","val0");
  1430.     ($tmp,@tmp3)=&Date_Regexp($week,"","val0");
  1431.     @tmp=reverse(@tmp,@tmp2,@tmp3);
  1432.     ($tmp,%Date::Manip::Week)=&Date_Regexp(\@tmp,"lc,under","1");
  1433.  
  1434.     $Date::Manip::DayExp=&Date_Regexp(join(" ",@days),"back,sort,lc,under");
  1435.     ($tmp,@Date::Manip::Day)=&Date_Regexp($days[0],"lc,under",1);
  1436.     %Date::Manip::Day  =();
  1437.     %Date::Manip::Which=();
  1438.     @tmp2=@tmp3=();
  1439.     foreach $days (@days) {
  1440.       ($tmp,%tmp) =&Date_Regexp($days,"lc,under","val1");
  1441.       @tmp=();
  1442.       foreach (1,2,3,4,5) {
  1443.         push(@tmp,($_,$tmp{$_}))  if (exists $tmp{$_});
  1444.       }
  1445.       push(@tmp3,reverse (@tmp));
  1446.  
  1447.       @tmp=%tmp;
  1448.       push(@tmp2,reverse(@tmp));
  1449.     }
  1450.     %Date::Manip::Day=@tmp2;
  1451.     ($tmp,@tmp) =&Date_Regexp($last,"lc,under",1);
  1452.     push(@tmp3, map { $_,-1 } @tmp);
  1453.     ($Date::Manip::WhichExp,@tmp)= &Date_Regexp(\@tmp3,"sort,back","keys");
  1454.     %Date::Manip::Which=@tmp3;
  1455.  
  1456.     $Date::Manip::AmPmExp=&Date_Regexp("$am $pm","lc,back,under");
  1457.     ($Date::Manip::AmExp,@tmp2)=&Date_Regexp("$am","lc,back,under",1);
  1458.     ($Date::Manip::PmExp,@tmp3)=&Date_Regexp("$pm","lc,back,under",1);
  1459.     @tmp=map { $_,1 } @tmp2;
  1460.     push(@tmp,map { $_,2 } @tmp3);
  1461.     %Date::Manip::AmPm=@tmp;
  1462.     ($tmp,@tmp2)=&Date_Regexp("$am","under",1);
  1463.     ($tmp,@tmp3)=&Date_Regexp("$pm","under",1);
  1464.     $Date::Manip::Am=shift(@tmp2);
  1465.     $Date::Manip::Pm=shift(@tmp3);
  1466.  
  1467.     $Date::Manip::YExp   =&Date_Regexp($years,"lc,sort,under");
  1468.     $Date::Manip::MExp   =&Date_Regexp($months,"lc,sort,under");
  1469.     $Date::Manip::WExp   =&Date_Regexp($weeks,"lc,sort,under");
  1470.     $Date::Manip::DExp   =&Date_Regexp($days,"lc,sort,under");
  1471.     $Date::Manip::HExp   =&Date_Regexp($hours,"lc,sort,under");
  1472.     $Date::Manip::MnExp  =&Date_Regexp($minutes,"lc,sort,under");
  1473.     $Date::Manip::SExp   =&Date_Regexp($seconds,"lc,sort,under,opt");
  1474.     %Date::Manip::Replace=split(/\s+/,lc($replace));
  1475.  
  1476.     $Date::Manip::Now=   &Date_Regexp($now,"lc,back,under");
  1477.     ($Date::Manip::Offset,%Date::Manip::Offset)=
  1478.       &Date_Regexp($offset,"lc,under,back","keys");
  1479.     ($Date::Manip::TimesExp,%Date::Manip::Times)=
  1480.       &Date_Regexp($times,"lc,under,back","keys");
  1481.     $Date::Manip::SepHM=$sephm;
  1482.     $Date::Manip::SepMS=$sepms;
  1483.     $Date::Manip::SepSS=$sepss;
  1484.  
  1485.     $zonesrfc=
  1486.       "idlw -1200 ".  # International Date Line West
  1487.       "nt   -1100 ".  # Nome
  1488.       "hst  -1000 ".  # Hawaii Standard
  1489.       "cat  -1000 ".  # Central Alaska
  1490.       "ahst -1000 ".  # Alaska-Hawaii Standard
  1491.       "yst  -0900 ".  # Yukon Standard
  1492.       "hdt  -0900 ".  # Hawaii Daylight
  1493.       "ydt  -0800 ".  # Yukon Daylight
  1494.       "pst  -0800 ".  # Pacific Standard
  1495.       "pdt  -0700 ".  # Pacific Daylight
  1496.       "mst  -0700 ".  # Mountain Standard
  1497.       "mdt  -0600 ".  # Mountain Daylight
  1498.       "cst  -0600 ".  # Central Standard
  1499.       "cdt  -0500 ".  # Central Daylight
  1500.       "est  -0500 ".  # Eastern Standard
  1501.       "edt  -0400 ".  # Eastern Daylight
  1502.       "ast  -0400 ".  # Atlantic Standard
  1503.       "nft  -0330 ".  # Newfoundland
  1504.       "bst  -0300 ".  # Brazil Standard             bst=British Summer   +0100
  1505.       "adt  -0300 ".  # Atlantic Daylight
  1506.       "ndt  -0230 ".  # Newfoundland Daylight
  1507.       "at   -0200 ".  # Azores
  1508.       "wat  -0100 ".  # West Africa
  1509.       "gmt  +0000 ".  # Greenwich Mean
  1510.       "ut   +0000 ".  # Universal (Coordinated)
  1511.       "utc  +0000 ".  # Universal (Coordinated)
  1512.       "wet  +0000 ".  # Western European
  1513.       "cet  +0100 ".  # Central European
  1514.       "fwt  +0100 ".  # French Winter
  1515.       "met  +0100 ".  # Middle European
  1516.       "mewt +0100 ".  # Middle European Winter
  1517.       "swt  +0100 ".  # Swedish Winter
  1518.       "eet  +0200 ".  # Eastern Europe, USSR Zone 1
  1519.       "fst  +0200 ".  # French Summer
  1520.       "mest +0200 ".  # Middle European Summer
  1521.       "sst  +0200 ".  # Swedish Summer              sst=South Sumatra    +0700
  1522.       "bt   +0300 ".  # Baghdad, USSR Zone 2
  1523.       "it   +0330 ".  # Iran
  1524.       "zp4  +0400 ".  # USSR Zone 3
  1525.       "zp5  +0500 ".  # USSR Zone 4
  1526.       "ist  +0530 ".  # Indian Standard
  1527.       "zp6  +0600 ".  # USSR Zone 5
  1528.       "nst  +0630 ".  # North Sumatra               nst=Newfoundland Std -0330
  1529.       "wast +0700 ".  # West Australian Standard
  1530.       "jt   +0730 ".  # Java (3pm in Cronusland!)
  1531.       "cct  +0800 ".  # China Coast, USSR Zone 7
  1532.       "wadt +0800 ".  # West Australian Daylight
  1533.       "jst  +0900 ".  # Japan Standard, USSR Zone 8
  1534.       "cast +0930 ".  # Central Australian Standard
  1535.       "east +1000 ".  # Eastern Australian Standard
  1536.       "gst  +1000 ".  # Guam Standard, USSR Zone 9  gst=Greenland Std    -0300
  1537.       "cadt +1030 ".  # Central Australian Daylight
  1538.       "eadt +1100 ".  # Eastern Australian Daylight
  1539.       "idle +1200 ".  # International Date Line East
  1540.       "nzst +1200 ".  # New Zealand Standard
  1541.       "nzt  +1200 ".  # New Zealand
  1542.       "nzdt +1300 ".  # New Zealand Daylight
  1543.       "z +0000 ".
  1544.       "a -0100 b -0200 c -0300 d -0400 e -0500 f -0600 g -0700 h -0800 ".
  1545.       "i -0900 k -1000 l -1100 m -1200 ".
  1546.       "n +0100 o +0200 p +0300 q +0400 r +0500 s +0600 t +0700 u +0800 ".
  1547.       "v +0900 w +1000 x +1100 y +1200 ".
  1548.       '[+-]\d{4} 0000';
  1549.     ($Date::Manip::ZoneExp,%Date::Manip::Zone)=
  1550.       &Date_Regexp("$zonesrfc $zones","sort,lc,under,back,opt,PRE,POST",
  1551.                    "keys");
  1552.     $tmp=
  1553.       "US/Pacific  PST8PDT ".
  1554.       "US/Mountain MST7MDT ".
  1555.       "US/Central  CST6CDT ".
  1556.       "US/Eastern  EST5EDT";
  1557.     ($Date::Manip::CurrZoneExp,%Date::Manip::CurrZone)=
  1558.       &Date_Regexp($tmp,"lc,under,back","keys");
  1559.     $Date::Manip::TZ=&Date_TimeZone;
  1560.  
  1561.     $Date::Manip::At    =&Date_Regexp($at,"lc,under,pre,post,optws");
  1562.     $Date::Manip::In    =&Date_Regexp($in,"lc,under,pre,post");
  1563.     $Date::Manip::On    =&Date_Regexp($on,"lc,under,pre,post,optws");
  1564.     $Date::Manip::Future=&Date_Regexp($future,"lc,under");
  1565.     $Date::Manip::Past  =&Date_Regexp($past,"lc,under");
  1566.     $Date::Manip::Next  =&Date_Regexp($next,"lc,under");
  1567.     $Date::Manip::Prev  =&Date_Regexp($prev,"lc,under");
  1568.  
  1569.     $Date::Manip::Exact   =&Date_Regexp($exact,"lc,under");
  1570.     $Date::Manip::Approx  =&Date_Regexp($approx,"lc,under");
  1571.     $Date::Manip::Business=&Date_Regexp($business,"lc,under");
  1572.  
  1573.   }
  1574.  
  1575.   if ($Date::Manip::ResetWorkDay) {
  1576.     my($h1,$m1,$h2,$m2)=();
  1577.     if ($Date::Manip::WorkDay24Hr) {
  1578.       ($Date::Manip::WDBh,$Date::Manip::WDBm)=(0,0);
  1579.       ($Date::Manip::WDEh,$Date::Manip::WDEm)=(24,0);
  1580.       $Date::Manip::WDlen=24*60;
  1581.       $Date::Manip::WorkDayBeg="00:00";
  1582.       $Date::Manip::WorkDayEnd="23:59";
  1583.  
  1584.     } else {
  1585.       confess "ERROR: Invalid WorkDayBeg in Date::Manip.\n"
  1586.         if (! (($h1,$m1)=&CheckTime($Date::Manip::WorkDayBeg)));
  1587.       confess "ERROR: Invalid WorkDayEnd in Date::Manip.\n"
  1588.         if (! (($h2,$m2)=&CheckTime($Date::Manip::WorkDayEnd)));
  1589.  
  1590.       ($Date::Manip::WDBh,$Date::Manip::WDBm)=($h1,$m1);
  1591.       ($Date::Manip::WDEh,$Date::Manip::WDEm)=($h2,$m2);
  1592.  
  1593.       $h1=$h2-$h1;
  1594.       $m1=$m2-$m1;
  1595.       if ($m1<0) {
  1596.         $h1--;
  1597.         $m1+=60;
  1598.       }
  1599.       $Date::Manip::WDlen=$h1*60+$m1;
  1600.     }
  1601.     $Date::Manip::ResetWorkDay=0;
  1602.   }
  1603.  
  1604.   my($s,$mn,$h,$d,$m,$y,$wday,$yday,$isdst)=localtime(time);
  1605.   $y+=1900;
  1606.   my($ampm)=();
  1607.   $wk="";
  1608.   $m++;
  1609.   &Date_ErrorCheck(\$y,\$m,\$d,\$h,\$mn,\$s,\$ampm,\$wk);
  1610.   $Date::Manip::CurrY=$y;
  1611.   $Date::Manip::CurrM=$m;
  1612.   $Date::Manip::CurrD=$d;
  1613.   $Date::Manip::CurrH=$h;
  1614.   $Date::Manip::CurrMn=$mn;
  1615.   $Date::Manip::CurrS=$s;
  1616.   $Date::Manip::CurrAmPm=$ampm;
  1617.   $Date::Manip::Curr=&FormDate($y,$m,$d,$h,$mn,$s);
  1618. }
  1619.  
  1620. sub Date_IsWorkDay {
  1621.   my($date,$time)=@_;
  1622.   &Date_Init()  if (! $Date::Manip::InitDone);
  1623.   my($d)=$date;
  1624.   $d=&Date_SetTime($date,$Date::Manip::WorkDayBeg)
  1625.     if (! defined $time  or  ! $time);
  1626.  
  1627.   my($y,$dofw,$h,$m)=&UnixDate($d,"%Y","%w","%H","%M");
  1628.   return 0  if ($dofw<$Date::Manip::WorkWeekBeg or
  1629.                 $dofw>$Date::Manip::WorkWeekEnd or
  1630.                 "$h:$m" lt $Date::Manip::WorkDayBeg or
  1631.                 "$h:$m" gt $Date::Manip::WorkDayEnd);
  1632.   if ($y!=$Date::Manip::CurrHolidayYear) {
  1633.     $Date::Manip::CurrHolidayYear=$y;
  1634.     &Date_UpdateHolidays;
  1635.   }
  1636.   $d=&Date_SetTime($date,"00:00:00");
  1637.   return 0  if (exists $Date::Manip::CurrHolidays{$d});
  1638.   1;
  1639. }
  1640.  
  1641. sub Date_NextWorkDay {
  1642.   my($date,$off,$time)=@_;
  1643.   &Date_Init()  if (! $Date::Manip::InitDone);
  1644.   my($err)=();
  1645.  
  1646.   if (! &Date_IsWorkDay($date,$time)) {
  1647.     if (defined $time and $time) {
  1648.       while (1) {
  1649.         $date=&Date_GetNext($date,undef,0,$Date::Manip::WorkDayBeg);
  1650.         last  if (&Date_IsWorkDay($date,$time));
  1651.       }
  1652.     } else {
  1653.       while (1) {
  1654.         $date=&DateCalc_DateDelta($date,"+0:0:1:0:0:0",\$err,0);
  1655.         last  if (&Date_IsWorkDay($date,$time));
  1656.       }
  1657.     }
  1658.   }
  1659.  
  1660.   while ($off>0) {
  1661.     while (1) {
  1662.       $date=&DateCalc_DateDelta($date,"+0:0:1:0:0:0",\$err,0);
  1663.       last  if (&Date_IsWorkDay($date,$time));
  1664.     }
  1665.     $off--;
  1666.   }
  1667.  
  1668.   return $date;
  1669. }
  1670.  
  1671. sub Date_PrevWorkDay {
  1672.   my($date,$off,$time)=@_;
  1673.   &Date_Init()  if (! $Date::Manip::InitDone);
  1674.   my($err)=();
  1675.  
  1676.   if (! &Date_IsWorkDay($date,$time)) {
  1677.     if (defined $time and $time) {
  1678.       while (1) {
  1679.         $date=&Date_GetPrev($date,undef,0,$Date::Manip::WorkDayEnd);
  1680.         last  if (&Date_IsWorkDay($date,$time));
  1681.       }
  1682.       while (1) {
  1683.         $date=&Date_GetNext($date,undef,0,$Date::Manip::WorkDayBeg);
  1684.         last  if (&Date_IsWorkDay($date,$time));
  1685.       }
  1686.     } else {
  1687.       while (1) {
  1688.         $date=&DateCalc_DateDelta($date,"-0:0:1:0:0:0",\$err,0);
  1689.         last  if (&Date_IsWorkDay($date,$time));
  1690.       }
  1691.     }
  1692.   }
  1693.  
  1694.   while ($off>0) {
  1695.     while (1) {
  1696.       $date=&DateCalc_DateDelta($date,"-0:0:1:0:0:0",\$err,0);
  1697.       last  if (&Date_IsWorkDay($date,$time));
  1698.     }
  1699.     $off--;
  1700.   }
  1701.  
  1702.   return $date;
  1703. }
  1704.  
  1705.  
  1706. sub Date_Regexp {
  1707.   my($list,$options,$array)=@_;
  1708.   my(@list,$ret,%hash,$i)=();
  1709.   $options=""  if (! defined $options);
  1710.   $array=""    if (! defined $array);
  1711.  
  1712.   my($sort,$lc,$under)=(0,0,0);
  1713.   $sort =1  if ($options =~ /sort/i);
  1714.   $lc   =1  if ($options =~ /lc/i);
  1715.   $under=1  if ($options =~ /under/i);
  1716.   my($back,$opt,$pre,$post,$ws)=("?:","","","","");
  1717.   $back =""          if ($options =~ /back/i);
  1718.   $opt  ="?"         if ($options =~ /opt/i);
  1719.   $pre  ='\s*'       if ($options =~ /pre/);
  1720.   $pre  ='\s+'       if ($options =~ /Pre/);
  1721.   $pre  ='(?:\s+|^)' if ($options =~ /PRE/);
  1722.   $post ='\s*'       if ($options =~ /post/);
  1723.   $post ='\s+'       if ($options =~ /Post/);
  1724.   $post ='(?:$|\s+)' if ($options =~ /POST/);
  1725.   $ws   ='\s*'       if ($options =~ /optws/);
  1726.   $ws   ='\s+'       if ($options =~ /optws/);
  1727.  
  1728.   my($hash,$keys,$key0,$key1,$val0,$val1)=(0,0,0,0,0,0);
  1729.   $keys =1     if ($array =~ /keys/i);
  1730.   $key0 =1     if ($array =~ /key0/i);
  1731.   $key1 =1     if ($array =~ /key1/i);
  1732.   $val0 =1     if ($array =~ /val0/i);
  1733.   $val1 =1     if ($array =~ /val1/i);
  1734.   $hash =1     if ($keys or $key0 or $key1 or $val0 or $val1);
  1735.  
  1736.   my($ref)=ref $list;
  1737.   if (! $ref) {
  1738.     $list =~ s/\s*$//;
  1739.     $list =~ s/^\s*//;
  1740.     $list =~ s/\s+/&&&/g;
  1741.   } elsif ($ref eq "ARRAY") {
  1742.     $list = join("&&&",@$list);
  1743.   } else {
  1744.     confess "ERROR: Date_Regexp.\n";
  1745.   }
  1746.  
  1747.   if (! $list) {
  1748.     if ($back eq "") {
  1749.       return "()";
  1750.     } else {
  1751.       return "";
  1752.     }
  1753.   }
  1754.  
  1755.   $list=lc($list)  if ($lc);
  1756.   $list=~ s/_/ /g  if ($under);
  1757.   @list=split(/&&&/,$list);
  1758.   if ($keys) {
  1759.     %hash=@list;
  1760.     @list=keys %hash;
  1761.   } elsif ($key0 or $key1 or $val0 or $val1) {
  1762.     $i=0;
  1763.     $i=1  if ($key1 or $val1);
  1764.     if ($key0 or $key1) {
  1765.       %hash= map { $_,$i++ } @list;
  1766.     } else {
  1767.       %hash= map { $i++,$_ } @list;
  1768.     }
  1769.   }
  1770.   @list=sort sortByLength(@list)  if ($sort);
  1771.  
  1772.   $ret="($back" . join("|",@list) . ")";
  1773.   $ret="(?:$pre$ret$post)"  if ($pre or $post);
  1774.   $ret.=$opt;
  1775.   $ret="(?:$ret|$ws)"  if ($ws);
  1776.  
  1777.   if ($array and $hash) {
  1778.     return ($ret,%hash);
  1779.   } elsif ($array) {
  1780.     return ($ret,@list);
  1781.   } else {
  1782.     return $ret;
  1783.   }
  1784. }
  1785.  
  1786. sub NormalizeDelta {
  1787.   my($delta,$mode)=@_;
  1788.   return "" if (! defined $delta  or  ! $delta);
  1789.   return "+0:+0:+0:+0:+0:+0"
  1790.     if ($delta =~ /^([+-]?0+:){5}[+-]?0+/ and $Date::Manip::DeltaSigns);
  1791.   return "+0:0:0:0:0:0" if ($delta =~ /^([+-]?0+:){5}[+-]?0+/);
  1792.  
  1793.   my($tmp,$sign1,$sign2,$len)=();
  1794.  
  1795.   $len=24*60;
  1796.   $len=$Date::Manip::WDlen  if ($mode==2);
  1797.  
  1798.  
  1799.   my($y,$mon,$d,$h,$m,$s)=&CheckDelta($delta);
  1800.  
  1801.  
  1802.  
  1803.   $mon += $y*12;                         # convert y to m
  1804.   $sign1="+";
  1805.   if ($mon<0) {
  1806.     $mon *= -1;
  1807.     $sign1="-";
  1808.   }
  1809.  
  1810.   $y    = ($tmp=int($mon/12));           # convert m to y
  1811.   $mon -= $tmp*12;
  1812.  
  1813.   $y=0    if ($y eq "-0");               # get around silly -0 problem
  1814.   $mon=0  if ($mon eq "-0");
  1815.  
  1816.  
  1817.   $s += $d*$len*60 + $h*3600 + $m*60;    # convert d/h/m to s
  1818.   $sign2="+";
  1819.   if ($s<0) {
  1820.     $s*=-1;
  1821.     $sign2="-";
  1822.   }
  1823.  
  1824.   $m  = ($tmp=int($s/60));               # convert s to m
  1825.   $s -= $tmp*60;
  1826.   $d  = ($tmp=int($m/$len));             # convert m to d
  1827.   $m -= $tmp*$len;
  1828.   $h  = ($tmp=int($m/60));               # convert m to h
  1829.   $m -= $tmp*60;
  1830.  
  1831.   $d=0    if ($d eq "-0");               # get around silly -0 problem
  1832.   $h=0    if ($h eq "-0");
  1833.   $m=0    if ($m eq "-0");
  1834.   $s=0    if ($s eq "-0");
  1835.  
  1836.   $sign1=$sign2  if ($y==0 and $mon==0);
  1837.   $sign2=$sign1  if ($d==0 and $h==0 and $m==0 and $s==0);
  1838.   $sign2=""  if ($sign1 eq $sign2  and  ! $Date::Manip::DeltaSigns);
  1839.  
  1840.   if ($Date::Manip::DeltaSigns) {
  1841.     return "$sign1$y:$sign1$mon:$sign2$d:$sign2$h:$sign2$m:$sign2$s";
  1842.   } else {
  1843.     return "$sign1$y:$mon:$sign2$d:$h:$m:$s";
  1844.   }
  1845. }
  1846.  
  1847. sub CheckDelta {
  1848.   my($delta,$sign,$exact)=@_;
  1849.   my(@delta)=split(/:/,$delta);
  1850.   return ()  if (defined $exact  and  $exact  and $#delta != 5);
  1851.   my($i)=();
  1852.   $sign="+"  if (! defined $sign);
  1853.   for ($i=0; $i<=$#delta; $i++) {
  1854.     $delta[$i]="0"  if (! $delta[$i]);
  1855.     return ()  if ($delta[$i] !~ /^[+-]?\d+$/);
  1856.     $sign = ($delta[$i] =~ s/^([+-])// ? $1 : $sign);
  1857.     $delta[$i] = $sign.$delta[$i];
  1858.   }
  1859.   @delta;
  1860. }
  1861.  
  1862. sub ParseTime {
  1863.   my($h,$m,$s)=@_;
  1864.   my($t)=&CheckTime("one");
  1865.  
  1866.   if (defined $h  and  $h =~ /$t/) {
  1867.     $h=$1;
  1868.     $m=$2;
  1869.     $s=$3   if (defined $3);
  1870.   }
  1871.   $h="00"  if (! defined $h);
  1872.   $m="00"  if (! defined $m);
  1873.   $s="00"  if (! defined $s);
  1874.  
  1875.   ($h,$m,$s);
  1876. }
  1877.  
  1878. sub FormDate {
  1879.   my($y,$m,$d,$h,$mn,$s)=@_;
  1880.   my($ym,$md,$dh,$hmn,$mns)=();
  1881.  
  1882.   if      ($Date::Manip::Internal == 0) {
  1883.     $ym=$md=$dh="";
  1884.     $hmn=$mns=":";
  1885.  
  1886.   } elsif ($Date::Manip::Internal == 1) {
  1887.     $ym=$md=$dh=$hmn=$mns="";
  1888.  
  1889.   } elsif ($Date::Manip::Internal == 2) {
  1890.     $ym=$md="-";
  1891.     $dh=" ";
  1892.     $hmn=$mns=":";
  1893.  
  1894.   } else {
  1895.     confess "ERROR: Invalid internal format in Date_FormDate.\n";
  1896.   }
  1897.   $m="0$m"    if (length($m)==1);
  1898.   $d="0$d"    if (length($d)==1);
  1899.   $h="0$h"    if (length($h)==1);
  1900.   $mn="0$mn"  if (length($mn)==1);
  1901.   $s="0$s"    if (length($s)==1);
  1902.   "$y$ym$m$md$d$dh$h$hmn$mn$mns$s";
  1903. }
  1904.  
  1905. sub CheckTime {
  1906.   my($time)=@_;
  1907.   my($h)='(?:0?[0-9]|1[0-9]|2[0-3])';
  1908.   my($h2)='(?:0[0-9]|1[0-9]|2[0-3])';
  1909.   my($m)='[0-5][0-9]';
  1910.   my($s)=$m;
  1911.   my($hm)="(?:$Date::Manip::SepHM|:)";
  1912.   my($ms)="(?:$Date::Manip::SepMS|:)";
  1913.   my($ss)=$Date::Manip::SepSS;
  1914.   my($t)="^($h)$hm($m)(?:$ms($s)(?:$ss\d+)?)?\$";
  1915.   if ($time eq "one") {
  1916.     return $t;
  1917.   } elsif ($time eq "two") {
  1918.     $t="^($h2)$hm($m)(?:$ms($s)(?:$ss\d+)?)?\$";
  1919.     return $t;
  1920.   }
  1921.  
  1922.   if ($time =~ /$t/i) {
  1923.     ($h,$m,$s)=($1,$2,$3);
  1924.     $h="0$h" if (length($h)<2);
  1925.     $m="0$m" if (length($m)<2);
  1926.     $s="00"  if (! defined $s);
  1927.     return ($h,$m,$s);
  1928.   } else {
  1929.     return ();
  1930.   }
  1931. }
  1932.  
  1933. sub CheckDate {
  1934.   my($date)=@_;
  1935.   my($ym,$md,$dh,$hmn,$mns)=();
  1936.   my($y)='(\d{4})';
  1937.   my($m)='(0[1-9]|1[0-2])';
  1938.   my($d)='(0[1-9]|[1-2][0-9]|3[0-1])';
  1939.   my($h)='([0-1][0-9]|2[0-3])';
  1940.   my($mn)='([0-5][0-9])';
  1941.   my($s)=$mn;
  1942.  
  1943.   if      ($Date::Manip::Internal == 0) {
  1944.     $ym=$md=$dh="";
  1945.     $hmn=$mns=":";
  1946.  
  1947.   } elsif ($Date::Manip::Internal == 1) {
  1948.     $ym=$md=$dh=$hmn=$mns="";
  1949.  
  1950.   } elsif ($Date::Manip::Internal == 2) {
  1951.     $ym=$md="-";
  1952.     $dh=" ";
  1953.     $hmn=$mns=":";
  1954.  
  1955.   } else {
  1956.     confess "ERROR: Invalid internal format in Date_CheckDate.\n";
  1957.   }
  1958.  
  1959.   my($t)="^$y$ym$m$md$d$dh$h$hmn$mn$mns$s\$";
  1960.   return $t  if (! $date);
  1961.  
  1962.   return ($1,$2,$3,$4,$5,$6)  if ($date =~ /$t/);
  1963.   return ();
  1964. }
  1965.  
  1966. sub DateCalc_DateDate {
  1967.   my($D1,$D2,$mode)=@_;
  1968.   my(@d_in_m)=(0,31,28,31,30,31,30,31,31,30,31,30,31);
  1969.   $mode=0  if (! defined $mode);
  1970.  
  1971.   if ($mode==0) {
  1972.     my($y1,$m1,$d1,$h1,$mn1,$s1)=&CheckDate($D1);
  1973.     my($y2,$m2,$d2,$h2,$mn2,$s2)=&CheckDate($D2);
  1974.     my($i,@delta,$d,$delta,$y)=();
  1975.  
  1976.     $delta[3]=$h2-$h1;
  1977.     $delta[4]=$mn2-$mn1;
  1978.     $delta[5]=$s2-$s1;
  1979.  
  1980.     $delta[0]=$delta[1]=0;
  1981.     $d=0;
  1982.     if ($y2>$y1) {
  1983.       $d=&Date_DaysInYear($y1) - &Date_DayOfYear($m1,$d1,$y1);
  1984.       $d+=&Date_DayOfYear($m2,$d2,$y2);
  1985.       for ($y=$y1+1; $y<$y2; $y++) {
  1986.         $d+= &Date_DaysInYear($y);
  1987.       }
  1988.     } elsif ($y2<$y1) {
  1989.       $d=&Date_DaysInYear($y2) - &Date_DayOfYear($m2,$d2,$y2);
  1990.       $d+=&Date_DayOfYear($m1,$d1,$y1);
  1991.       for ($y=$y2+1; $y<$y1; $y++) {
  1992.         $d+= &Date_DaysInYear($y);
  1993.       }
  1994.       $d *= -1;
  1995.     } else {
  1996.       $d=&Date_DayOfYear($m2,$d2,$y2) - &Date_DayOfYear($m1,$d1,$y1);
  1997.     }
  1998.     $delta[2]=$d;
  1999.  
  2000.     for ($i=0; $i<6; $i++) {
  2001.       $delta[$i]="+".$delta[$i]  if ($delta[$i]>=0);
  2002.     }
  2003.  
  2004.     $delta=join(":",@delta);
  2005.     $delta=&NormalizeDelta($delta,0);
  2006.     return $delta;
  2007.   }
  2008.  
  2009.   my($date1,$date2)=($D1,$D2);
  2010.   my($tmp,$sign,$err,@tmp)=();
  2011.  
  2012.   if ($mode==2) {
  2013.     $date1=&Date_NextWorkDay($date1,0,1);
  2014.     $date2=&Date_NextWorkDay($date2,0,1);
  2015.   }
  2016.  
  2017.   if ($date1 gt $date2) {
  2018.     $sign="-";
  2019.     $tmp=$date1;
  2020.     $date1=$date2;
  2021.     $date2=$tmp;
  2022.   } else {
  2023.     $sign="+";
  2024.   }
  2025.   if ($date1 eq $date2) {
  2026.     return "+0:+0:+0:+0:+0:+0"  if ($Date::Manip::DeltaSigns);
  2027.     return "+0:0:0:0:0:0";
  2028.   }
  2029.  
  2030.   my($y1,$m1,$d1,$h1,$mn1,$s1)=&CheckDate($date1);
  2031.   my($y2,$m2,$d2,$h2,$mn2,$s2)=&CheckDate($date2);
  2032.   my($dy,$dm,$dd,$dh,$dmn,$ds,$ddd)=();
  2033.  
  2034.   $dy=$y2-$y1;
  2035.   $dm=0;
  2036.   if ($dy>0) {
  2037.     $tmp=&DateCalc_DateDelta($date1,"+$dy:0:0:0:0:0",\$err,0);
  2038.     if ($tmp gt $date2) {
  2039.       $dy--;
  2040.       $tmp=$date1;
  2041.       $tmp=&DateCalc_DateDelta($date1,"+$dy:0:0:0:0:0",\$err,0)  if ($dy>0);
  2042.       $dm=12;
  2043.     }
  2044.     $date1=$tmp;
  2045.   }
  2046.  
  2047.   $dm+=$m2-$m1;
  2048.   if ($dm>0) {
  2049.     $tmp=&DateCalc_DateDelta($date1,"+0:$dm:0:0:0:0",\$err,0);
  2050.     if ($tmp gt $date2) {
  2051.       $dm--;
  2052.       $tmp=$date1;
  2053.       $tmp=&DateCalc_DateDelta($date1,"+0:$dm:0:0:0:0",\$err,0)  if ($dm>0);
  2054.     }
  2055.     $date1=$tmp;
  2056.   }
  2057.  
  2058.   if ($mode==2) {
  2059.     $dd=0;
  2060.     while (1) {
  2061.       $tmp=&Date_NextWorkDay($date1,1,1);
  2062.       if ($tmp le $date2) {
  2063.         $dd++;
  2064.         $date1=$tmp;
  2065.       } else {
  2066.         last;
  2067.       }
  2068.     }
  2069.  
  2070.   } else {
  2071.     ($y1,$m1,$d1)=( &CheckDate($date1) )[0..2];
  2072.     $dd=0;
  2073.     if ($m1!=$m2) {
  2074.       $d_in_m[2]=29  if (&Date_LeapYear($y1));
  2075.       $dd=$d_in_m[$m1]-$d1+1;
  2076.       $d1=1;
  2077.       $tmp=&DateCalc_DateDelta($date1,"+0:0:$dd:0:0:0",\$err,0);
  2078.       if ($tmp gt $date2) {
  2079.         $dd--;
  2080.         $d1--;
  2081.         $tmp=&DateCalc_DateDelta($date1,"+0:0:$dd:0:0:0",\$err,0);
  2082.       }
  2083.       $date1=$tmp;
  2084.     }
  2085.  
  2086.     $ddd=0;
  2087.     if ($d1<$d2) {
  2088.       $ddd=$d2-$d1;
  2089.       $tmp=&DateCalc_DateDelta($date1,"+0:0:$ddd:0:0:0",\$err,0);
  2090.       if ($tmp gt $date2) {
  2091.         $ddd--;
  2092.         $tmp=&DateCalc_DateDelta($date1,"+0:0:$ddd:0:0:0",\$err,0);
  2093.       }
  2094.       $date1=$tmp;
  2095.     }
  2096.     $dd+=$ddd;
  2097.   }
  2098.  
  2099.   $d1=( &CheckDate($date1) )[2];
  2100.   $dh=$dmn=$ds=0;
  2101.   if ($mode==2  and  $d1<$d2) {
  2102.     $tmp=&Date_SetTime($date1,$Date::Manip::WorkDayEnd);
  2103.     $tmp=&DateCalc_DateDelta($tmp,"+0:0:0:0:1:0")
  2104.       if ($Date::Manip::WorkDay24Hr);
  2105.     $tmp=&DateCalc_DateDate($date1,$tmp,0);
  2106.     ($tmp,$tmp,$tmp,$dh,$dmn,$ds)=&CheckDelta($tmp);
  2107.     $date1=&Date_NextWorkDay($date1,1,0);
  2108.     $date1=&Date_SetTime($date1,$Date::Manip::WorkDayBeg);
  2109.     $d1=( &CheckDate($date1) )[2];
  2110.     confess "ERROR: DateCalc DateDate Business.\n"  if ($d1 != $d2);
  2111.   }
  2112.  
  2113.   $tmp=&DateCalc_DateDate($date1,$date2,0);
  2114.   @tmp=&CheckDelta($tmp);
  2115.   $dh  += $tmp[3];
  2116.   $dmn += $tmp[4];
  2117.   $ds  += $tmp[5];
  2118.  
  2119.   if ($Date::Manip::DeltaSigns) {
  2120.     return "$sign$dy:$sign$dm:$sign$dd:$sign$dh:$sign$dmn:$sign$ds";
  2121.   } else {
  2122.     return "$sign$dy:$dm:$dd:$dh:$dmn:$ds";
  2123.   }
  2124. }
  2125.  
  2126. sub DateCalc_DeltaDelta {
  2127.   my($D1,$D2,$mode)=@_;
  2128.   my(@delta1,@delta2,$i,$delta,@delta)=();
  2129.   $mode=0  if (! defined $mode);
  2130.  
  2131.   @delta1=&CheckDelta($D1);
  2132.   @delta2=&CheckDelta($D2);
  2133.   for ($i=0; $i<6; $i++) {
  2134.     $delta[$i]=$delta1[$i]+$delta2[$i];
  2135.     $delta[$i]="+".$delta[$i]  if ($delta[$i]>=0);
  2136.   }
  2137.  
  2138.   $delta=join(":",@delta);
  2139.   $delta=&NormalizeDelta($delta,$mode);
  2140.   return $delta;
  2141. }
  2142.  
  2143. sub DateCalc_DateDelta {
  2144.   my($D1,$D2,$errref,$mode)=@_;
  2145.   my($date)=();
  2146.   my(@d_in_m)=(0,31,28,31,30,31,30,31,31,30,31,30,31);
  2147.   my($h1,$m1,$h2,$m2,$len,$hh,$mm)=();
  2148.   $mode=0  if (! defined $mode);
  2149.  
  2150.   if ($mode==2) {
  2151.     $h1=$Date::Manip::WDBh;
  2152.     $m1=$Date::Manip::WDBm;
  2153.     $h2=$Date::Manip::WDEh;
  2154.     $m2=$Date::Manip::WDEm;
  2155.     $hh=$h2-$h1;
  2156.     $mm=$m2-$m1;
  2157.     if ($mm<0) {
  2158.       $hh--;
  2159.       $mm+=60;
  2160.     }
  2161.   }
  2162.  
  2163.   my($y,$m,$d,$h,$mn,$s)=&CheckDate($D1);
  2164.   my($dy,$dm,$dd,$dh,$dmn,$ds)=&CheckDelta($D2);
  2165.  
  2166.   $y+=$dy;
  2167.   &ModuloAddition(-12,$dm,\$m,\$y);   # -12 means 1-12 instead of 0-11
  2168.   $d_in_m[2]=29  if (&Date_LeapYear($y));
  2169.  
  2170.   if ($mode==2) {
  2171.     $d=$d_in_m[$m] if ($d>$d_in_m[$m]);
  2172.     $date=&Date_NextWorkDay(&FormDate($y,$m,$d,$h,$mn,$s),0,1);
  2173.     ($y,$m,$d,$h,$mn,$s)=&CheckDate($date);
  2174.   }
  2175.  
  2176.   &ModuloAddition(60,$ds,\$s,\$mn);
  2177.   if ($mode==2) {
  2178.     while (1) {
  2179.       &ModuloAddition(60,$dmn,\$mn,\$h);
  2180.       $h+= $dh;
  2181.  
  2182.       if ($h>$h2  or  $h==$h2 && $mn>$m2) {
  2183.         $dh=$h-$h2;
  2184.         $dmn=$mn-$m2;
  2185.         $h=$h1;
  2186.         $mn=$m1;
  2187.         $dd++;
  2188.  
  2189.       } elsif ($h<$h1  or  $h==$h1 && $mn<$m1) {
  2190.         $dh=$h1-$h;
  2191.         $dmn=$m1-$mn;
  2192.         $h=$h2;
  2193.         $mn=$m2;
  2194.         $dd--;
  2195.  
  2196.       } elsif ($h==$h2  &&  $mn==$m2) {
  2197.         $dd++;
  2198.         $dh=-$hh;
  2199.         $dmn=-$mm;
  2200.  
  2201.       } else {
  2202.         last;
  2203.       }
  2204.     }
  2205.  
  2206.   } else {
  2207.     &ModuloAddition(60,$dmn,\$mn,\$h);
  2208.     &ModuloAddition(24,$dh,\$h,\$d);
  2209.   }
  2210.  
  2211.   if ($d>$d_in_m[$m]) {
  2212.     $dd+= $d-$d_in_m[$m];
  2213.     $d=$d_in_m[$m];
  2214.   }
  2215.  
  2216.   if ($mode==2) {
  2217.     if ($dd>=0) {
  2218.       $date=&Date_NextWorkDay(&FormDate($y,$m,$d,$h,$mn,$s),$dd,1);
  2219.     } else {
  2220.       $date=&Date_PrevWorkDay(&FormDate($y,$m,$d,$h,$mn,$s),-$dd,1);
  2221.     }
  2222.     ($y,$m,$d,$h,$mn,$s)=&CheckDate($date);
  2223.  
  2224.   } else {
  2225.     $d_in_m[2]=29  if (&Date_LeapYear($y));
  2226.     $d=$d_in_m[$m]  if ($d>$d_in_m[$m]);
  2227.     $d += $dd;
  2228.     while ($d<1) {
  2229.       $m--;
  2230.       if ($m==0) {
  2231.         $m=12;
  2232.         $y--;
  2233.         if (&Date_LeapYear($y)) {
  2234.           $d_in_m[2]=29;
  2235.         } else {
  2236.           $d_in_m[2]=28;
  2237.         }
  2238.       }
  2239.       $d += $d_in_m[$m];
  2240.     }
  2241.     while ($d>$d_in_m[$m]) {
  2242.       $d -= $d_in_m[$m];
  2243.       $m++;
  2244.       if ($m==13) {
  2245.         $m=1;
  2246.         $y++;
  2247.         if (&Date_LeapYear($y)) {
  2248.           $d_in_m[2]=29;
  2249.         } else {
  2250.           $d_in_m[2]=28;
  2251.         }
  2252.       }
  2253.     }
  2254.   }
  2255.  
  2256.   if ($y<1000 or $y>9999) {
  2257.     $$errref=3;
  2258.     return;
  2259.   }
  2260.   &FormDate($y,$m,$d,$h,$mn,$s);
  2261. }
  2262.  
  2263. sub Date_UpdateHolidays {
  2264.   my($date,$delta,$err)=();
  2265.   local($_)=();
  2266.   foreach (keys %Date::Manip::Holidays) {
  2267.     if (/^(.*)([+-].*)$/) {
  2268.       ($date,$delta)=($1,$2);
  2269.       $Date::Manip::UpdateHolidays=1;
  2270.       $date=&ParseDate($date);
  2271.       $Date::Manip::UpdateHolidays=0;
  2272.       $date=&DateCalc($date,$delta,\$err,0);
  2273.  
  2274.     } else {
  2275.       $Date::Manip::UpdateHolidays=1;
  2276.       $date=&ParseDate($_);
  2277.       $Date::Manip::UpdateHolidays=0;
  2278.     }
  2279.     $Date::Manip::CurrHolidays{$date}=1;
  2280.   }
  2281. }
  2282.  
  2283. sub Date_SetConfigVariable {
  2284.   my($var,$val)=@_;
  2285.  
  2286.   return  if ($var =~ /^PersonalCnf$/i);
  2287.   return  if ($var =~ /^PersonalCnfPath$/i);
  2288.  
  2289.   $Date::Manip::InitFilesRead=1,   return  if ($var =~ /^IgnoreGlobalCnf$/i);
  2290.   %Date::Manip::Holidays=(),       return  if ($var =~ /^EraseHolidays$/i);
  2291.   $Date::Manip::Init=0,
  2292.   $Date::Manip::Language=$val,     return  if ($var =~ /^Language$/i);
  2293.   $Date::Manip::DateFormat=$val,   return  if ($var =~ /^DateFormat$/i);
  2294.   $Date::Manip::TZ=$val,           return  if ($var =~ /^TZ$/i);
  2295.   $Date::Manip::ConvTZ=$val,       return  if ($var =~ /^ConvTZ$/i);
  2296.   $Date::Manip::Internal=$val,     return  if ($var =~ /^Internal$/i);
  2297.   $Date::Manip::FirstDay=$val,     return  if ($var =~ /^FirstDay$/i);
  2298.   $Date::Manip::WorkWeekBeg=$val,  return  if ($var =~ /^WorkWeekBeg$/i);
  2299.   $Date::Manip::WorkWeekEnd=$val,  return  if ($var =~ /^WorkWeekEnd$/i);
  2300.   $Date::Manip::WorkDayBeg=$val,
  2301.   $Date::Manip::ResetWorkDay=1,    return  if ($var =~ /^WorkDayBeg$/i);
  2302.   $Date::Manip::WorkDayEnd=$val,
  2303.   $Date::Manip::ResetWorkDay=1,    return  if ($var =~ /^WorkDayEnd$/i);
  2304.   $Date::Manip::WorkDay24Hr=$val,
  2305.   $Date::Manip::ResetWorkDay=1,    return  if ($var =~ /^WorkDay24Hr$/i);
  2306.   $Date::Manip::DeltaSigns=$val,   return  if ($var =~ /^DeltaSigns$/i);
  2307.  
  2308.   confess "ERROR: Unknown configuration variable $var in Date::Manip.\n";
  2309. }
  2310.  
  2311. sub ReadInitFile {
  2312.   my($file)=@_;
  2313.   local($_)=();
  2314.   my($section)="vars";
  2315.   my($var,$val,$date,$name)=();
  2316.  
  2317.   open(IN,$file);
  2318.   while(defined ($_=<IN>)) {
  2319.     chomp;
  2320.     s/^\s+//;
  2321.     s/\s+$//;
  2322.     next  if (! $_  or  /^\#/);
  2323.     if (s/^\*\s*//) {
  2324.       $section=$_;
  2325.       next;
  2326.     }
  2327.  
  2328.     if ($section =~ /var/) {
  2329.       confess "ERROR: invalid Date::Manip config file line.\n  $_\n"
  2330.         if (! /(.*\S)\s*=\s*(.*)$/);
  2331.       ($var,$val)=($1,$2);
  2332.       &Date_SetConfigVariable($var,$val);
  2333.  
  2334.     } elsif ($section =~ /holiday/i) {
  2335.       confess "ERROR: invalid Date::Manip config file line.\n  $_\n"
  2336.         if (! /(.*\S)\s*=\s*(.*)$/);
  2337.       ($date,$name)=($1,$2);
  2338.       $name=""  if (! defined $name);
  2339.       $Date::Manip::Holidays{$date}=$name;
  2340.  
  2341.     } else {
  2342.       next;
  2343.     }
  2344.   }
  2345.   close(IN);
  2346. }
  2347.  
  2348. no strict "vars";
  2349. sub sortByLength {
  2350.   return (length $b <=> length $a);
  2351. }
  2352. use strict "vars";
  2353.  
  2354. sub Date_ErrorCheck {
  2355.   my($y,$m,$d,$h,$mn,$s,$ampm,$wk)=@_;
  2356.   my($tmp1,$tmp2,$tmp3)=();
  2357.  
  2358.   my(@d_in_m)=(0,31,28,31,30,31,30,31,31,30,31,30,31);
  2359.   my($curr_y)=$Date::Manip::CurrY;
  2360.   my($curr_m)=$Date::Manip::CurrM;
  2361.   my($curr_d)=$Date::Manip::CurrD;
  2362.   $$y=""     if (! defined $$y);
  2363.   $$m=""     if (! defined $$m);
  2364.   $$d=""     if (! defined $$d);
  2365.   $$h=""     if (! defined $$h);
  2366.   $$mn=""    if (! defined $$mn);
  2367.   $$s=""     if (! defined $$s);
  2368.   $$ampm=""  if (! defined $$ampm);
  2369.   $$ampm=uc($$ampm)  if ($$ampm);
  2370.   $$wk=""    if (! defined $$wk);
  2371.   $$d=$curr_d  if ($$y eq "" and $$m eq "" and $$d eq "");
  2372.  
  2373.   $$y=$Date::Manip::CurrHolidayYear  if ($Date::Manip::UpdateHolidays);
  2374.   $$y=$curr_y    if ($$y eq "");
  2375.   if (length($$y)==2) {
  2376.     $tmp1=$curr_y-89;
  2377.     $$y="19$$y";
  2378.     while ($$y<$tmp1) {
  2379.       $$y+=100;
  2380.     }
  2381.   }
  2382.   return 1       if (! &IsInt($$y,1,9999));
  2383.   $d_in_m[2]=29  if (&Date_LeapYear($$y));
  2384.  
  2385.   $$m=$curr_m     if ($$m eq "");
  2386.   $$m=$Date::Manip::Month{lc($$m)}  if (exists $Date::Manip::Month{lc($$m)});
  2387.   $$m="0$$m"      if (length($$m)==1);
  2388.   return 1        if (! &IsInt($$m,1,12));
  2389.  
  2390.   $$d="01"        if ($$d eq "");
  2391.   $$d="0$$d"      if (length($$d)==1);
  2392.   return 1        if (! &IsInt($$d,1,$d_in_m[$$m]));
  2393.   if ($$wk) {
  2394.     $tmp1=&Date_DayOfWeek($$m,$$d,$$y);
  2395.     $tmp2=$Date::Manip::Week{lc($$wk)}
  2396.       if (exists $Date::Manip::Week{lc($$wk)});
  2397.     return 1      if ($tmp1 != $tmp2);
  2398.   }
  2399.  
  2400.   $tmp1=$Date::Manip::AmPmExp;
  2401.   $tmp2="";
  2402.   if ($$ampm =~ /^$tmp1$/i) {
  2403.     $tmp3=$Date::Manip::AmExp;
  2404.     $tmp2="AM"  if ($$ampm =~ /^$tmp3$/i);
  2405.     $tmp3=$Date::Manip::PmExp;
  2406.     $tmp2="PM"  if ($$ampm =~ /^$tmp3$/i);
  2407.   } elsif ($$ampm) {
  2408.     return 1;
  2409.   }
  2410.   if ($tmp2 eq "AM" || $tmp2 eq "PM") {
  2411.     $$h="0$$h"    if (length($$h)==1);
  2412.     return 1      if ($$h<1 || $$h>12);
  2413.     $$h="00"      if ($tmp2 eq "AM"  and  $$h==12);
  2414.     $$h += 12     if ($tmp2 eq "PM"  and  $$h!=12);
  2415.   } else {
  2416.     $$h="00"      if ($$h eq "");
  2417.     $$h="0$$h"    if (length($$h)==1);
  2418.     return 1      if (! &IsInt($$h,0,23));
  2419.     $tmp2="AM"    if ($$h<12);
  2420.     $tmp2="PM"    if ($$h>=12);
  2421.   }
  2422.   $$ampm=$Date::Manip::Am;
  2423.   $$ampm=$Date::Manip::Pm  if ($tmp2 eq "PM");
  2424.  
  2425.   $$mn="00"       if ($$mn eq "");
  2426.   $$mn="0$$mn"    if (length($$mn)==1);
  2427.   return 1        if (! &IsInt($$mn,0,59));
  2428.  
  2429.   $$s="00"        if ($$s eq "");
  2430.   $$s="0$$s"      if (length($$s)==1);
  2431.   return 1        if (! &IsInt($$s,0,59));
  2432.  
  2433.   return 0;
  2434. }
  2435.  
  2436.  
  2437. sub ModuloAddition {
  2438.   my($N,$add,$val,$rem)=@_;
  2439.   return  if ($N==0);
  2440.   $$val+=$add;
  2441.   if ($N<0) {
  2442.     $N = -$N;
  2443.     if ($$val>$N) {
  2444.       $$rem+= int(($$val-1)/$N);
  2445.       $$val = ($$val-1)%$N +1;
  2446.     } elsif ($$val<1) {
  2447.       $$rem-= int(-$$val/$N)+1;
  2448.       $$val = $N-(-$$val % $N);
  2449.     }
  2450.  
  2451.   } else {
  2452.     if ($$val>($N-1)) {
  2453.       $$rem+= int($$val/$N);
  2454.       $$val = $$val%$N;
  2455.     } elsif ($$val<0) {
  2456.       $$rem-= int(-($$val+1)/$N)+1;
  2457.       $$val = ($N-1)-(-($$val+1)%$N);
  2458.     }
  2459.   }
  2460. }
  2461.  
  2462. sub IsInt {
  2463.   my($N,$low,$high)=@_;
  2464.   return 0 if ($N eq "");
  2465.   my($sign)='^\s* [-+]? \s*';
  2466.   my($int) ='\d+ \s* $ ';
  2467.   if ($N =~ /$sign $int/x) {
  2468.     if (defined $low  and  defined $high) {
  2469.       return 1  if ($N>=$low  and  $N<=$high);
  2470.       return 0;
  2471.     }
  2472.     return 1;
  2473.   }
  2474.   return 0;
  2475. }
  2476.  
  2477. sub SinLindex {
  2478.   my($listref,$Str,$Offset,$Insensitive)=@_;
  2479.   my($i,$len,$tmp)=();
  2480.   $len=$#$listref;
  2481.   return -2  if ($len<0 or ! $Str);
  2482.   return -1  if (&Index_First(\$Offset,$len));
  2483.   $Str=uc($Str)  if ($Insensitive);
  2484.   for ($i=$Offset; $i<=$len; $i++) {
  2485.     $tmp=$$listref[$i];
  2486.     $tmp=uc($tmp)  if ($Insensitive);
  2487.     return $i  if ($tmp eq $Str);
  2488.   }
  2489.   return -1;
  2490. }
  2491. sub Index_First {
  2492.   my($Offsetref,$max)=@_;
  2493.   $$Offsetref=0  if (! $$Offsetref);
  2494.   if ($$Offsetref < 0) {
  2495.     $$Offsetref += $max + 1;
  2496.     $$Offsetref=0  if ($$Offsetref < 0);
  2497.   }
  2498.   return -1 if ($$Offsetref > $max);
  2499.   return 0;
  2500. }
  2501.  
  2502. sub CleanFile {
  2503.   my($file)=@_;
  2504.   $file =~ s/\s*$//;
  2505.   $file =~ s/^\s*//;
  2506.   $file =~ s|//+|/|g;  # multiple slash
  2507.   $file =~ s|/\.$|/|;  # trailing /. (leaves trailing slash)
  2508.   $file =~ s|^\./||    # leading ./
  2509.     if ($file ne "./");
  2510.   $file =~ s|/$||      # trailing slash
  2511.     if ($file ne "/");
  2512.   return $file;
  2513. }
  2514.  
  2515. sub ExpandTilde {
  2516.   my($file)=shift;
  2517.   my($user)=();
  2518.   my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)=();
  2519.   if ($file =~ m% ^~ ([^\/]*) (\/.*)? %x) {
  2520.     ($user,$file)=($1,$2);
  2521.     return ""  if (defined $^O and
  2522.                    $^O =~ /MacOS/i ||
  2523.                    $^O =~ /MSWin32/i ||
  2524.                    $^O =~ /Windows_95/i);
  2525.     return ""  if (defined $ENV{OS} and
  2526.                    $ENV{OS} =~ /MacOS/i ||
  2527.                    $ENV{OS} =~ /MSWin32/i ||
  2528.                    $ENV{OS} =~ /Windows_95/i);
  2529.     $user=""  if (! defined $user);
  2530.     $file=""  if (! defined $file);
  2531.     if ($user) {
  2532.       ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)=
  2533.         getpwnam($user);
  2534.     } else {
  2535.       ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)=
  2536.         getpwuid($<);
  2537.     }
  2538.     return ""  if (! $dir);
  2539.  
  2540.     $file="$dir/$file";
  2541.   }
  2542.   return &CleanFile($file);
  2543. }
  2544.  
  2545. sub FullFilePath {
  2546.   my($file)=shift;
  2547.   $file=&ExpandTilde($file);
  2548.   return ""  if (! $file);
  2549.   $file=cwd . "/$file"  if ($file !~ m|^/|);   # $file = "a/b/c"
  2550.   return &CleanFile($file);
  2551. }
  2552.  
  2553. sub CheckFilePath {
  2554.   my($file,$mode)=@_;
  2555.   my($test)=();
  2556.   $file=&FullFilePath($file);
  2557.   $mode = ""  if (! defined $mode);
  2558.  
  2559.   return "" if (! defined $file  or  ! $file  or  ! -e $file );
  2560.  
  2561.   foreach $test ("r","w","x","R","W","X","o","O","e","z","s","f","d","l","s",
  2562.                  "p","b","c","u","g","k","T","B") {
  2563.     return 0  if ($mode =~ /$test/  and  ! eval "-$test '$file'");
  2564.   }
  2565.  
  2566.   return 1;
  2567. }
  2568.  
  2569. sub FixPath {
  2570.   my($path,$full,$mode,$err)=@_;
  2571.   my(@dir)=split(/:/,$path);
  2572.   $full=0  if (! defined $full);
  2573.   $mode="" if (! defined $mode);
  2574.   $err=0   if (! defined $err);
  2575.   $path="";
  2576.   if ($mode eq "e") {
  2577.     $mode="de";
  2578.   } elsif ($mode eq "r") {
  2579.     $mode="derx";
  2580.   } elsif ($mode eq "w") {
  2581.     $mode="derwx";
  2582.   }
  2583.  
  2584.   foreach (@dir) {
  2585.  
  2586.     if ($full) {
  2587.       $_=&FullFilePath($_);
  2588.     } else {
  2589.       $_=&ExpandTilde($_);
  2590.     }
  2591.     if (! $_) {
  2592.       return ""  if ($err);
  2593.       next;
  2594.     }
  2595.  
  2596.     if (! $mode  or  &CheckFilePath($_,$mode)) {
  2597.       $path .= ":$_";
  2598.     } else {
  2599.       return "" if ($err);
  2600.     }
  2601.   }
  2602.   $path =~ s/^://;
  2603.   return $path;
  2604. }
  2605.  
  2606. sub SearchPath {
  2607.   my($file,$path,$mode,@suff)=@_;
  2608.   my($f,$s,$d,@dir,$fs)=();
  2609.   $path=&FixPath($path,1,"r");
  2610.   @dir=split(/:/,$path);
  2611.   foreach $d (@dir) {
  2612.     $f="$d/$file";
  2613.     $f=~ s|//|/|g;
  2614.     return $f if (&CheckFilePath($f,$mode));
  2615.     foreach $s (@suff) {
  2616.       $fs="$f.$s";
  2617.       return $fs if (&CheckFilePath($fs,$mode));
  2618.     }
  2619.   }
  2620.   return "";
  2621. }
  2622.  
  2623.  
  2624. 1;
  2625.  
  2626.  
  2627. =head1 NAME
  2628.  
  2629. Date::Manip - date manipulation routines
  2630.  
  2631. =head1 SYNOPSIS
  2632.  
  2633.  use Date::Manip;
  2634.  
  2635.  $date=&ParseDate(\@args)
  2636.  $date=&ParseDate($string)
  2637.  $date=&ParseDate(\$string)
  2638.  
  2639.  @date=&UnixDate($date,@format)
  2640.  $date=&UnixDate($date,@format)
  2641.  
  2642.  $delta=&ParseDateDelta(\@args)
  2643.  $delta=&ParseDateDelta($string)
  2644.  $delta=&ParseDateDelta(\$string)
  2645.  
  2646.  $d=&DateCalc($d1,$d2,$errref,$del)
  2647.  
  2648.  $date=&Date_SetTime($date,$hr,$min,$sec)
  2649.  $date=&Date_SetTime($date,$time)
  2650.  
  2651.  $date=&Date_GetPrev($date,$dow,$today,$hr,$min,$sec)
  2652.  $date=&Date_GetPrev($date,$dow,$today,$time)
  2653.  
  2654.  $date=&Date_GetNext($date,$dow,$today,$hr,$min,$sec)
  2655.  $date=&Date_GetNext($date,$dow,$today,$time)
  2656.  
  2657.  &Date_Init()
  2658.  &Date_Init("VAR=VAL",...)
  2659.  
  2660.  $version=&DateManipVersion
  2661.  
  2662.  $flag=&Date_IsWorkDay($date [,$flag]);
  2663.  
  2664.  $date=&Date_NextWorkDay($date,$off [,$time]);
  2665.  $date=&Date_PrevWorkDay($date,$off [,$time]);
  2666.  
  2667. The following routines are used by the above routines (though they can
  2668. also be called directly).  Make sure that $y is entered as the full 4
  2669. digit year (it will die if a 2 digit years is entered).  Most (if not
  2670. all) of the information below can be gotten from UnixDate which is really
  2671. the way I intended it to be gotten.
  2672.  
  2673.  $day=&Date_DayOfWeek($m,$d,$y)
  2674.  $secs=&Date_SecsSince1970($m,$d,$y,$h,$mn,$s)
  2675.  $secs=&Date_SecsSince1970GMT($m,$d,$y,$h,$mn,$s)
  2676.  $days=&Date_DaysSince999($m,$d,$y)
  2677.  $day=&Date_DayOfYear($m,$d,$y)
  2678.  $days=&Date_DaysInYear($y)
  2679.  $wkno=&Date_WeekOfYear($m,$d,$y,$first)
  2680.  $flag=&Date_LeapYear($y)
  2681.  $day=&Date_DaySuffix($d)
  2682.  $tz=&Date_TimeZone()
  2683.  
  2684. =head1 DESCRIPTION
  2685.  
  2686. This is a set of routines designed to make any common date/time
  2687. manipulation easy to do.  Operations such as comparing two times,
  2688. calculating a time a given amount of time from another, or parsing
  2689. international times are all easily done.
  2690.  
  2691. Date::Manip deals only with the Gregorian calendar (the one currently in
  2692. use).  The Julian calendar defined leap years as every 4th year.  The
  2693. Gregorian calendar improved this by making every 100th year NOT a leap
  2694. year, unless it was also the 400th year.  The Gregorian calendar has been
  2695. extrapolated back to the year 1000 AD and forward to the year 9999 AD.
  2696. Note that in historical context, the Julian calendar was in use until 1582
  2697. when the Gregorian calendar was adopted by the Catholic church.  Protestant
  2698. countries did not accept it until later; Germany and Netherlands in 1698,
  2699. British Empire in 1752, Russia in 1918.  Note that the Gregorian calendar
  2700. is itself imperfect.  Each year is on average 26 seconds too long, which
  2701. means that every 3,323 years, a day should be removed from the calendar.
  2702. No attempt is made to correct for that.
  2703.  
  2704. Date::Manip is therefore not equipped to truly deal with historacle dates,
  2705. but should be able to perform (virtually) any operation dealing with a
  2706. modern time and date.
  2707.  
  2708. Among other things, Date::Manip allow you to:
  2709.  
  2710. 1.  Enter a date and be able to choose any format conveniant
  2711.  
  2712. 2.  Compare two dates, entered in widely different formats to determine
  2713.     which is earlier
  2714.  
  2715. 3.  Extract any information you want from ANY date using a format string
  2716.     similar to the Unix date command
  2717.  
  2718. 4.  Determine the amount of time between two dates
  2719.  
  2720. 5.  Add a time offset to a date to get a second date (i.e. determine the
  2721.     date 132 days ago or 2 years and 3 months after Jan 2, 1992)
  2722.  
  2723. 6.  Work with dates with dates using international formats (foreign month
  2724.     names, 12-10-95 referring to October rather than December, etc.).
  2725.  
  2726. Each of these tasks is trivial (one or two lines at most) with this package.
  2727.  
  2728. Although the word date is used extensively here, it is actually somewhat
  2729. misleading.  Date::Manip works with the full date AND time (year, month,
  2730. day, hour, minute, second).
  2731.  
  2732. In the documentation below, US formats are used, but in most (if not all)
  2733. cases, a non-English equivalent will work equally well.
  2734.  
  2735. =head1 EXAMPLES
  2736.  
  2737. 1.  Parsing a date from any conveniant format
  2738.  
  2739.   $date=&ParseDate("today");
  2740.   $date=&ParseDate("1st thursday in June 1992");
  2741.   $date=&ParseDate("05-10-93");
  2742.   $date=&ParseDate("12:30 Dec 12th 1880");
  2743.   $date=&ParseDate("8:00pm december tenth");
  2744.   if (! $date) {
  2745.   }
  2746.  
  2747. 2.  Compare two dates
  2748.  
  2749.   $date1=&ParseDate($string1);
  2750.   $date2=&ParseDate($string2);
  2751.   if ($date1 lt $date2) {
  2752.   } else {
  2753.   }
  2754.  
  2755. 3.  Extract information from a date.
  2756.  
  2757.   print &UnixDate("today","The time is now %T on %b %e, %Y.");
  2758.   =>  "The time is now 13:24:08 on Feb  3, 1996."
  2759.  
  2760. 4.  The amount of time between two dates.
  2761.  
  2762.   $date1=&ParseDate($string1);
  2763.   $date2=&ParseDate($string2);
  2764.   $delta=&DateCalc($date1,$date2,\$err);
  2765.   => 0:0:DD:HH:MM:SS   the days, hours, minutes, and seconds between the two
  2766.   $delta=&DateCalc($date1,$date2,\$err,1);
  2767.   => YY:MM:DD:HH:MM:SS  the years, months, etc. between the two
  2768.  
  2769.   Read the documentation below for an explanation of the difference.
  2770.  
  2771. 5.  To determine a date a given offset from another.
  2772.  
  2773.   $date=&DateCalc("today","+ 3hours 12minutes 6 seconds",\$err);
  2774.   $date=&DateCalc("12 hours ago","12:30 6Jan90",\$err);
  2775.  
  2776.   It even works with business days:
  2777.  
  2778.   $date=&DateCalc("today","+ 3 business days",\$err);
  2779.  
  2780. 6.  To work with dates in another language.
  2781.  
  2782.   &Date_Init("Language=French","DateFormat=non-US");
  2783.   $date=&ParseDate("1er decembre 1990");
  2784.  
  2785. NOTE: Some date forms do not work as well in languages other than English,
  2786. but this is not because DateManip is incapable of doing so (almost nothing
  2787. in this module is language dependent).  It is simply that I do not have the
  2788. correct translation available for some words.  If there is a date form that
  2789. works in English but does not work in a language you need, let me know and
  2790. if you can provide me the translation, I will fix DateManip.
  2791.  
  2792. =head1 ROUTINES
  2793.  
  2794. =over 4
  2795.  
  2796. =item ParseDate
  2797.  
  2798.  $date=&ParseDate(\@args)
  2799.  $date=&ParseDate($string)
  2800.  $date=&ParseDate(\$string)
  2801.  
  2802. This takes an array or a string containing a date and parses it.  When the
  2803. date is included as an array (for example, the arguments to a program) the
  2804. array should contain a valid date in the first one or more elements
  2805. (elements after a valid date are ignored).  Elements containing a valid
  2806. date are shifted from the array.  The largest possible number of elements
  2807. which can be correctly interpreted as a valid date are always used.  If a
  2808. string is entered rather than an array, that string is tested for a valid
  2809. date.  The string is unmodified, even if passed in by reference.
  2810.  
  2811. A date actually includes 2 parts: date and time.  A time must include
  2812. hours and minutes and can optionally include seconds, fractional seconds,
  2813. an am/pm type string, and a timezone.  For example:
  2814.  
  2815.      HH:MN  [Zone]
  2816.      HH:MN:SS  [Zone]
  2817.      HH:MN am  [Zone]
  2818.      HH:MN:SS am  [Zone]
  2819.      HH:MN:SS:SSSS  [Zone]
  2820.      HH:MN:SS.SSSS am [Zone]
  2821.  
  2822. Hours can be written using 1 or 2 digits when the time follows the date and
  2823. is separated from the date with spaces or some other separator.  Any time
  2824. there is no space separating the time from a date and the part of the
  2825. date immediately preceding the hour is a digit, 2 digits must be used
  2826. for the hours.
  2827.  
  2828. Fractional seconds are also supported in parsing but the fractional part is
  2829. discarded.
  2830.  
  2831. Timezones always appear after the time and must be separated from all other
  2832. parts of the time/date by spaces.  For now, only rudimentary timezone
  2833. handling is done.  At the time the date is parsed, it is converted to a
  2834. specific time zone (which defaults to whatever time zone you are in, but
  2835. this can be overridden using the Date_Init routine described below).  After
  2836. that, the time zone is never used.  Once converted, information about the
  2837. time zone is no longer stored or used.
  2838.  
  2839. See the section below on TIMEZONEs for a list of all defined timezone
  2840. names.
  2841.  
  2842. Spaces in the date are almost always optional when there is absolutely
  2843. no ambiguity if they are not present.  Years can be entered as 2 or 4 digits,
  2844. days and months as 1 or 2 digits.  Both days and months must include 2 digits
  2845. whenver they are immediately adjacent to another part of the date or time
  2846. Valid formats for a full date and time (and examples of how Dec 10, 1965 at
  2847. 9:00 pm might appear) are:
  2848.   DateTime
  2849.      Date=YYMMDD             1965121021:00:00
  2850.                              65121021:00
  2851.  
  2852.   Date Time
  2853.   Date%Time
  2854.     Date=mm%dd, mm%dd%YY     12/10/65 21:00
  2855.                              12 10 1965 9:00pm
  2856.     Date=mmm%dd, mmm%dd%YY   December-10-65-9:00:00pm
  2857.     Date=dd%mmm, dd%mmm%YY   10/December/65 9:00:00pm
  2858.  
  2859.   Date Time
  2860.     Date=mmmdd, mmmdd YY,    Dec10 65 9:00:00 pm
  2861.          mmmDDYY, mmm DDYY   December 10 1965 9:00pm
  2862.  
  2863.     Date=ddmmm, ddmmm YY, ddmmmYY, dd mmmYY
  2864.                              10Dec65 9:00:00 pm     10 December 1965 9:00pm
  2865.  
  2866.   TimeDate
  2867.   Time Date
  2868.   Time%Date
  2869.     Date=mm%dd, mm%dd%YY     9:00pm 12.10.65      21:00 12/10/1965
  2870.     Date=mmm%dd, mmm%dd%YY   9:00pm December/10/65
  2871.     Date=dd%mmm, dd%mmm%YY   9:00pm 10-December-65  21:00/10/Dec/65
  2872.  
  2873.   TimeDate
  2874.   Time Date
  2875.     Date=mmmdd, mmmdd YY, mmmDDYY
  2876.                              21:00:00DeCeMbEr10
  2877.     Date=ddmmm, ddmmm YY, ddmmmYY, dd mmmYY
  2878.                              21:00 10Dec95
  2879.  
  2880. Miscellaneous other allowed formats are:
  2881.   which dofw in mmm [at time]
  2882.   which dofw in mmm YY [at time]    "first sunday in june 1996 at 14:00"
  2883.  
  2884.   dofw week num [in YY] [at time]   "sunday week 22 in 1995"
  2885.   which dofw [in YY] [at time]      "22nd sunday in 1996 at noon"
  2886.   dofw which week [in YY] [at time] "sunday 22nd week in 1996"
  2887.   next/last dofw [at time]          "next friday at noon"
  2888.   in num weeks [at time]            "in 3 weeks at 12:00"
  2889.   num weeks ago [at time]           "3 weeks ago"
  2890.   dofw in num week [at time]        "Friday in 2 weeks"
  2891.   in num weeks on dofw [at time]    "in 2 weeks on friday"
  2892.   dofw num week ago [at time]       "Friday 2 weeks ago"
  2893.   num week ago dofw [at time]       "2 weeks ago friday"
  2894.  
  2895. In addition, the following strings are recognized:
  2896.   today
  2897.   now       (synonym for today)
  2898.   yesterday (exactly 24 hours before now)
  2899.   tomorrow  (exactly 24 hours from now)
  2900.   noon      (12:00:00)
  2901.   midnight  (00:00:00)
  2902.  
  2903.  %       One of the valid date separators: - . / or whitespace (the same
  2904.          character must be used for all occurences of a single date)
  2905.          example: mm%dd%YY works for 1-1-95, 1 1 95, or 1/1/95
  2906.  YY      year in 2 or 4 digit format
  2907.  MM      two digit month (01 to 12)
  2908.  mm      one or two digit month (1 to 12 or 01 to 12)
  2909.  mmm     month name or 3 character abbreviation
  2910.  DD      two digit day (01 to 31)
  2911.  dd      one or two digit day (1 to 31 or 01 to 31)
  2912.  HH      one or two digit hour in 12 or 24 hour mode (0 to 23 or 00 to 23)
  2913.  MN      two digit minutes (00 to 59)
  2914.  SS      two digit seconds (00 to 59)
  2915.  which   one of the strings (first-fifth, 1st-5th, or last)
  2916.  dofw    either the 3 character abbreviation or full name of a day of
  2917.          the week
  2918.  
  2919. Some things to note:
  2920.  
  2921. All strings are case insensitive.  "December" and "DEceMBer" both work.
  2922.  
  2923. When a part of the date is not given, defaults are used: year defaults
  2924. to current year; hours, minutes, seconds to 00.
  2925.  
  2926. In the above, the mm%dd formats can be switched to dd%mm by calling
  2927. Date_Init and telling it to use a non-US date format.
  2928.  
  2929. All "Date Time" and "DateTime" type formats allow the word "at" in them
  2930. (i.e.  Jan 12 at 12:00) (and at can replace the space).  So the following
  2931. are both acceptable: "Jan 12at12:00" and "Jan 12 at 12:00".
  2932.  
  2933. A time is usually entered in 24 hour mode.  It can be followed by "am" or
  2934. "pm" to force it to be read in in 12 hour mode.
  2935.  
  2936. The year may be entered as 2 or 4 digits.  If entered as 2 digits, it is
  2937. taken to be the year in the range CurrYear-89 to CurrYear+10.  So, if the
  2938. current year is 1996, the range is [1907 to 2006] so entering the year 00
  2939. refers to 2000, 05 to 2005, but 07 refers to 1907.  Use 4 digit years to
  2940. avoid confusion!
  2941.  
  2942. Any number of spaces or tabs can be used anyhere whitespace is appropriate.
  2943.  
  2944. Dates are always checked to make sure they are valid.
  2945.  
  2946. In all of the formats, the day of week ("Friday") can be entered anywhere
  2947. in the date and it will be checked for accuracy.  In other words,
  2948.   "Tue Jul 16 1996 13:17:00"
  2949. will work but
  2950.   "Jul 16 1996 Wednesday 13:17:00"
  2951. will not (because Jul 16, 1996 is Tuesday, not Wednesday).  Note that
  2952. depending on where the weekday comes, it may give unexpected results when
  2953. used in array context.  For example, the date ("Jun","25","Sun","1990")
  2954. would return June 25 of the current year since Jun 25, 1990 is not Sunday.
  2955.  
  2956. The times "12:00 am", "12:00 pm", and "midnight" are not well defined.  For
  2957. good or bad, I use the following convention in Date::Manip:
  2958.   midnight = 12:00am = 00:00:00
  2959.   noon     = 12:00pm = 12:00:00
  2960. and the day goes from 00:00:00 to 23:59:59.  In otherwords, midnight is the
  2961. beginning of a day rather than the end of one.  At midnight on July 5, July
  2962. 5 has just begun.  The time 24:00:00 is NOT allowed.
  2963.  
  2964. The format of the date returned is YYYYMMDDHH:MM:SS.  The advantage of this
  2965. time format is that two times can be compared using simple string comparisons
  2966. to find out which is later.  Also, it is readily understood by a human.
  2967. Alternate forms can be used if that is more conveniant.  See Date_Init below
  2968. and the config variable Internal.
  2969.  
  2970. =item UnixDate
  2971.  
  2972.  @date=&UnixDate($date,@format)
  2973.  $date=&UnixDate($date,@format)
  2974.  
  2975. This takes a date and a list of strings containing formats roughly
  2976. identical to the format strings used by the UNIX date(1) command.  Each
  2977. format is parsed and an array of strings corresponding to each format is
  2978. returned.
  2979.  
  2980. $date must be of the form produced by &ParseDate.
  2981.  
  2982. The format options are:
  2983.  
  2984.  Year
  2985.      %y     year                     - 00 to 99
  2986.      %Y     year                     - 0001 to 9999
  2987.  Month, Week
  2988.      %m     month of year            - 01 to 12
  2989.      %f     month of year            - " 1" to "12"
  2990.      %b,%h  month abbreviation       - Jan to Dec
  2991.      %B     month name               - January to December
  2992.      %U     week of year, Sunday
  2993.             as first day of week     - 00 to 53
  2994.      %W     week of year, Monday
  2995.             as first day of week     - 00 to 53
  2996.  Day
  2997.      %j     day of the year          - 001 to 366
  2998.      %d     day of month             - 01 to 31
  2999.  
  3000.      %e     day of month             - " 1" to "31"
  3001.      %v     weekday abbreviation     - " S"," M"," T"," W","Th"," F","Sa"
  3002.      %a     weekday abbreviation     - Sun to Sat
  3003.      %A     weekday name             - Sunday to Saturday
  3004.      %w     day of week              - 0 (Sunday) to 6
  3005.      %E     day of month with suffix - 1st, 2nd, 3rd...
  3006.  Hour
  3007.      %H     hour                     - 00 to 23
  3008.      %k     hour                     - " 0" to "23"
  3009.      %i     hour                     - " 1" to "12"
  3010.      %I     hour                     - 01 to 12
  3011.      %p     AM or PM
  3012.  Minute, Second, Timezone
  3013.      %M     minute                   - 00 to 59
  3014.      %S     second                   - 00 to 59
  3015.      %s     seconds from Jan 1, 1970 GMT
  3016.                                      - negative if before 1/1/1970
  3017.      %o     seconds from Jan 1, 1970 in the current time zone
  3018.      %z,%Z  timezone (3 characters)  - "EDT"
  3019.  Date, Time
  3020.      %c     %a %b %e %H:%M:%S %Y     - Fri Apr 28 17:23:15 1995
  3021.      %C,%u  %a %b %e %H:%M:%S %z %Y  - Fri Apr 28 17:25:57 EDT 1995
  3022.      %g     %a, %d %b %Y %H:%M:%S %z - Fri, 28 Apr 1995 17:23:15 EDT
  3023.      %D,%x  %m/%d/%y                 - 04/28/95
  3024.      %l     date in ls(1) format
  3025.               %b %e $H:$M            - Apr 28 17:23  (if within 6 months)
  3026.               %b %e  %Y              - Apr 28  1993  (otherwise)
  3027.      %r     %I:%M:%S %p              - 05:39:55 PM
  3028.      %R     %H:%M                    - 17:40
  3029.      %T,%X  %H:%M:%S                 - 17:40:58
  3030.      %V     %m%d%H%M%y               - 0428174095
  3031.      %Q     %Y%m%d                   - 19961025
  3032.      %q     %Y%m%d%H%M%S             - 19961025174058
  3033.      %P     %Y%m%d%H%M%S             - 1996102517:40:58
  3034.      %F     %A, %B %e, %Y            - Sunday, January  1, 1996
  3035.  Other formats
  3036.      %n     insert a newline character
  3037.      %t     insert a tab character
  3038.      %%     insert a `%' character
  3039.      %+     insert a `+' character
  3040.  The following formats are currently unused but may be used in the future:
  3041.      GJKLNO 1234567890 !@#$^&*()_|-=\`[];',./~{}:<>?
  3042.  They currently insert the character following the %, but may (and probably
  3043.  will) change in the future as new formats are requested.
  3044.  
  3045. If a lone percent is the final character in a format, it is ignored.
  3046.  
  3047. Note that the ls format applies to date within the past OR future 6 months!
  3048.  
  3049. Note that the %s format was introduced in version 5.07.  Prior to that,
  3050. %s referred to the seconds since 1/1/70.  This was moved to %o in 5.07.
  3051.  
  3052. This routine is loosely based on date.pl (version 3.2) by Terry McGonigal.
  3053. No code was used, but most of his formats were.
  3054.  
  3055. =item ParseDateDelta
  3056.  
  3057.  $delta=&ParseDateDelta(\@args)
  3058.  $delta=&ParseDateDelta($string)
  3059.  $delta=&ParseDateDelta(\$string)
  3060.  
  3061. This takes an array and shifts a valid delta date (an amount of time)
  3062. from the array.  Recognized deltas are of the form:
  3063.   +Yy +Mm +Ww +Dd +Hh +MNmn +Ss
  3064.       examples:
  3065.          +4 hours +3mn -2second
  3066.          + 4 hr 3 minutes -2
  3067.          4 hour + 3 min -2 s
  3068.   +Y:+M:+D:+H:+MN:+S
  3069.       examples:
  3070.          0:0:0:4:3:-2
  3071.          +4:3:-2
  3072.   mixed format
  3073.       examples:
  3074.          4 hour 3:-2
  3075.  
  3076. A field in the format +Yy is a sign, a number, and a string specifying
  3077. the type of field.  The sign is "+", "-", or absent (defaults to the
  3078. next larger element).  The valid strings specifying the field type
  3079. are:
  3080.    y:  y, yr, year, years
  3081.    m:  m, mon, month, months
  3082.    w:  w, wk, ws, wks, week, weeks
  3083.    d:  d, day, days
  3084.    h:  h, hr, hour, hours
  3085.    mn: mn, min, minute, minutes
  3086.    s:  s, sec, second, seconds
  3087.  
  3088. Also, the "s" string may be omitted.  The sign, number, and string may
  3089. all be separated from each other by any number of whitespaces.
  3090.  
  3091. In the date, all fields must be given in the order: y m d h mn s.  Any
  3092. number of them may be omitted provided the rest remain in the correct
  3093. order.  In the 2nd (colon) format, from 2 to 6 of the fields may be given.
  3094. For example +D:+H:+MN:+S may be given to specify only four of the fields.
  3095. In any case, both the MN and S field may be present.  No spaces may be
  3096. present in the colon format.
  3097.  
  3098. Deltas may also be given as a combination of the two formats.  For example,
  3099. the following is valid: +Yy +D:+H:+MN:+S.  Again, all fields must be given
  3100. in the correct order.
  3101.  
  3102. The word "in" may be prepended to the delta ("in 5 years") and the word
  3103. "ago" may be appended ("6 months ago").  The "in" is completely ignored.
  3104. The "ago" has the affect of reversing all signs that appear in front of the
  3105. components of the delta.  I.e. "-12 yr 6 mon ago" is identical to "+12yr
  3106. +6mon" (don't forget that there is an impled minus sign in front of the 6
  3107. because when no sign is explicitely given, it carries the previously
  3108. entered sign).
  3109.  
  3110. The "week" field does not occur in the colon separated delta.  The reason
  3111. for this is to maintain backward compatibility with previous versions of
  3112. Date::Manip.  Parsing of weeks was only added in version 5.07.  At this
  3113. point, rather than change the internal format of the delta to
  3114. "Y:M:W:D:H:MN:S", I simply added the weeks to the days (1 week = 7 days) in
  3115. order to be compatible with previous versions.  So, they are not parsed in
  3116. the colon format, only in the first format.  Hopefully, this will not
  3117. result in too much confusion.
  3118.  
  3119. One thing is worth noting.  The year/month and day/hour/min/sec parts are
  3120. returned in a "normalized" form.  That is, the signs are adjusted so as to
  3121. be all positive or all negative.  For example, "+ 2 day - 2hour" does not
  3122. return "0:0:2:-2:0:0".  It returns "+0:0:1:22:0:0" (1 day 22 hours which is
  3123. equivalent).  I find (and I think most others agree) that this is a more
  3124. useful form.
  3125.  
  3126. Since the year/month and day/hour/min/sec parts must be normalized
  3127. separately there is the possibility that the sign of the two parts will be
  3128. different.  So, the delta "+ 2years -10 months - 2 days + 2 hours" produces
  3129. the delta "+1:2:-1:22:0:0".
  3130.  
  3131. For backwards compatibility, it is possible to include a sign for all
  3132. elements that is output.  See the configuration variable DeltaSigns below.
  3133.  
  3134. =item DateCalc
  3135.  
  3136.  $d=&DateCalc($d1,$d2,\$err [,$mode])
  3137.  
  3138. This takes two dates, deltas, or one of each and performs the appropriate
  3139. calculation with them.  Dates must be in the format given by &ParseDate and
  3140. or must be a string which can be parsed as a date.  Deltas must be in the
  3141. format returned by &ParseDateDelta or must be a string that can be parsed
  3142. as a delta.  Two deltas add together to form a third delta.  A date and a
  3143. delta returns a 2nd date.  Two dates return a delta (the difference between
  3144. the two dates).
  3145.  
  3146. Note that in many cases, it is somewhat ambiguous what the delta actually
  3147. refers to.  Although it is ALWAYS known how many months in a year, hours in
  3148. a day, etc., it is NOT known how many days form a month.  As a result, the
  3149. part of the delta containing month/year and the part with sec/min/hr/day
  3150. must be treated separately.  For example, "Mar 31, 12:00:00" plus a delta
  3151. of 1month 2days would yield "May 2 12:00:00".  The year/month is first
  3152. handled while keeping the same date.  Mar 31 plus one month is Apr 31 (but
  3153. since Apr only has 30 days, it becomes Apr 30).  Apr 30 + 2 days is May 2.
  3154. As a result, in the case where two dates are entered, the resulting delta
  3155. can take on two different forms.  By default ($mode=0), an absolutely
  3156. correct delta (ignoring daylight savings time) is returned in days, hours,
  3157. minutes, and seconds.
  3158.  
  3159. If $mode is 1, the math is done using an approximate mode where a delta is
  3160. returned using years and months as well.  The year and month part is
  3161. calculated first followed by the rest.  For example, the two dates "Mar 12
  3162. 1995" and "Apr 13 1995" would have an exact delta of "31 days" but in the
  3163. approximate mode, it would be returned as "1 month 1 day".  Also, "Mar 31"
  3164. and "Apr 30" would have deltas of "30 days" or "1 month" (since Apr 31
  3165. doesn't exist, it drops down to Apr 30).  Approximate mode is a more human
  3166. way of looking at things (you'd say 1 month and 2 days more often then 33
  3167. days), but it is less meaningful in terms of absolute time.  In approximate
  3168. mode $d1 and $d2 must be dates.  If either or both is a delta, the
  3169. calculation is done in exact mode.
  3170.  
  3171. If $mode is 2, a business mode is used.  That is, the calculation is done
  3172. using business days, ignoring holidays, weekends, etc.  In order to
  3173. correctly use this mode, a config file must exist which contains the
  3174. section defining holidays (see documentation on the config file below).
  3175. The config file can also define the work week and the hours of the work
  3176. day, so it is possible to have different config files for different
  3177. businesses.
  3178.  
  3179. For example, if a config file defines the workday as 08:00 to 18:00, a
  3180. workweek consisting of Mon-Sat, and the standard (American) holidays, then
  3181. from Tuesday at 12:00 to the following Monday at 14:00 is 5 days and 2
  3182. hours.  If the "end" of the day is reached in a calculation, it
  3183. autmoatically switches to the next day.  So, Tuesday at 12:00 plus 6 hours
  3184. is Wednesday at 08:00 (provided Wed is not a holiday).  Also, a date that
  3185. is not during a workday automatically becomes the start of the next
  3186. workday.  So, Sunday 12:00 and Monday at 03:00 both automatically becomes
  3187. Monday at 08:00 (provided Monday is not a holiday).  In business mode, any
  3188. combination of date and delta may be entered, but a delta should not
  3189. contain a year or month field (weeks are fine though).
  3190.  
  3191. See below for some additional comments about business mode calculations.
  3192.  
  3193. Any other non-nil value of $mode is treated as $mode=1 (approximate mode).
  3194.  
  3195. The mode can be automatically set in the dates/deltas passed by including a
  3196. key word somewhere in it.  For example, in English, if the word
  3197. "approximately" is found in either of the date/delta arguments, approximate
  3198. mode is forced.  Likewise, if the word "business" or "exactly" appears,
  3199. business/exact mode is forced (and $mode is ignored).  So, the two
  3200. following are equivalent:
  3201.  
  3202.    $date=&DateCalc("today","+ 2 business days",\$err);
  3203.    $date=&DateCalc("today","+ 2 days",\$err,2);
  3204.  
  3205. Note that if the keyword method is used instead of passing in $mode, it is
  3206. important that the keyword actually appear in the argument passed in to
  3207. DateCalc.  The following will NOT work:
  3208.  
  3209.    $delta=&ParseDateDelta("+ 2 business days");
  3210.    $today=&ParseDate("today");
  3211.    $date=&DateCalc($today,$delta,\$err);
  3212.  
  3213. because the mode keyword is removed from a date/delta by the parse routines,
  3214. and the mode is reset each time a parse routine is called.  Since DateCalc
  3215. parses both of its arguments, whatever mode was previously set is ignored.
  3216.  
  3217. $err is set to:
  3218.    1 is returned if $d1 is not a delta or date
  3219.    2 is returned if $d2 is not a delta or date
  3220.    3 is returned if the date is outside the years 1000 to 9999
  3221.  
  3222. Nothing is returned if an error occurs.
  3223.  
  3224. When a delta is returned, the signs such that it is strictly positive or
  3225. strictly negative ("1 day - 2 hours" would never be returned for example).
  3226. The only time when this cannot be enforced is when two deltas with a
  3227. year/month component are entered.  In this case, only the signs on the
  3228. day/hour/min/sec part are standardized.
  3229.  
  3230. =item Date_SetTime
  3231.  
  3232.  $date=&Date_SetTime($date,$hr,$min,$sec)
  3233.  $date=&Date_SetTime($date,$time)
  3234.  
  3235. This takes a date sets the time in that date.  For example, to get
  3236. the time for 7:30 tomorrow, use the lines:
  3237.  
  3238.    $date=&ParseDate("tomorrow")
  3239.    $date=&Date_SetTime($date,"7:30")
  3240.  
  3241. =item Date_GetPrev
  3242.  
  3243.  $date=&Date_GetPrev($date,$dow, $curr [,$hr,$min,$sec])
  3244.  $date=&Date_GetPrev($date,$dow, $curr [,$time])
  3245.  $date=&Date_GetPrev($date,undef,$curr,$hr,$min,$sec)
  3246.  $date=&Date_GetPrev($date,undef,$curr,$time)
  3247.  
  3248. If $dow is defined, it is a day of week (a string such as "Fri" or a number
  3249. from 0 to 6).  The date of the previous $dow is returned.  If $date falls
  3250. on this day of week, the date returned will be $date (if $curr is non-zero)
  3251. or a week earlier (if $curr is 0).  If a time is passed in (either as
  3252. separate hours, minutes, seconds or as a time in HH:MM:SS or HH:MM format),
  3253. the time on this date is set to it.  The following examples should
  3254. illustrate the use of Date_GetPrev:
  3255.  
  3256.     date                   dow    curr  time            returns
  3257.     Fri Nov 22 18:15:00    Thu    0     12:30           Thu Nov 21 12:30:00
  3258.     Fri Nov 22 18:15:00    Fri    0     12:30           Fri Nov 15 12:30:00
  3259.     Fri Nov 22 18:15:00    Fri    1     12:30           Fri Nov 22 12:30:00
  3260.  
  3261. If $dow is undefined, then a time must be entered, and the date returned is
  3262. the previous occurence of this time.  If $curr is non-zero, the current
  3263. time is returned if it matches the criteria passed in.  In other words, the
  3264. time returned is the last time that a digital clock (in 24 hour mode) would
  3265. have displayed the time you pass in.  If you define hours, minutes and
  3266. seconds default to 0 and you might jump back as much as an entire day.  If
  3267. hours are undefined, you are looking for the last time the minutes/seconds
  3268. appeared on the digital clock, so at most, the time will jump back one hour.
  3269.  
  3270.     date               curr  hr     min    sec      returns
  3271.     Nov 22 18:15:00    0/1   18     undef  undef    Nov 22 18:00:00
  3272.     Nov 22 18:15:00    0/1   18     30     0        Nov 21 18:30:00
  3273.     Nov 22 18:15:00    0     18     15     undef    Nov 21 18:15:00
  3274.     Nov 22 18:15:00    1     18     15     undef    Nov 22 18:15:00
  3275.     Nov 22 18:15:00    0     undef  15     undef    Nov 22 17:15:00
  3276.     Nov 22 18:15:00    1     undef  15     undef    Nov 22 18:15:00
  3277.  
  3278.  
  3279. =item Date_GetNext
  3280.  
  3281.  $date=&Date_GetNext($date,$dow, $curr [,$hr,$min,$sec])
  3282.  $date=&Date_GetNext($date,$dow, $curr [,$time])
  3283.  $date=&Date_GetNext($date,undef,$curr,$hr,$min,$sec)
  3284.  $date=&Date_GetNext($date,undef,$curr,$time)
  3285.  
  3286. Similar to Date_GetPrev.
  3287.  
  3288. =item Date_DayOfWeek
  3289.  
  3290.  $day=&Date_DayOfWeek($m,$d,$y);
  3291.  
  3292. Returns the day of the week (0 for Sunday, 6 for Saturday).  Dec 31, 0999
  3293. was Tuesday.
  3294.  
  3295. =item Date_SecsSince1970
  3296.  
  3297.  $secs=&Date_SecsSince1970($m,$d,$y,$h,$mn,$s)
  3298.  
  3299. Returns the number of seconds since Jan 1, 1970 00:00 (negative if date is
  3300. earlier).
  3301.  
  3302. =item Date_SecsSince1970GMT
  3303.  
  3304.  $secs=&Date_SecsSince1970GMT($m,$d,$y,$h,$mn,$s)
  3305.  
  3306. Returns the number of seconds since Jan 1, 1970 00:00 GMT (negative if date
  3307. is earlier).  If CurrTZ is "IGNORE", the number will be identical to
  3308. Date_SecsSince1970 (i.e. the date given will be treated as being in GMT).
  3309.  
  3310. =item Date_DaysSince999
  3311.  
  3312.  $days=&Date_DaysSince999($m,$d,$y)
  3313.  
  3314. Returns the number of days since Dec 31, 0999.
  3315.  
  3316. =item Date_DayOfYear
  3317.  
  3318.  $day=&Date_DayOfYear($m,$d,$y);
  3319.  
  3320. Returns the day of the year (001 to 366)
  3321.  
  3322. =item Date_DaysInYear
  3323.  
  3324.  $days=&Date_DaysInYear($y);
  3325.  
  3326. Returns the number of days in the year (365 or 366)
  3327.  
  3328. =item Date_WeekOfYear
  3329.  
  3330.  $wkno=&Date_WeekOfYear($m,$d,$y,$first);
  3331.  
  3332. Figure out week number.  $first is the first day of the week which is
  3333. usually 0 (Sunday) or 1 (Monday), but could be any number between 0 and 6
  3334. in practice.
  3335.  
  3336. =item Date_LeapYear
  3337.  
  3338.  $flag=&Date_LeapYear($y);
  3339.  
  3340. Returns 1 if the argument is a leap year
  3341. Written by David Muir Sharnoff <muir@idiom.com>
  3342.  
  3343. =item Date_DaySuffix
  3344.  
  3345.  $day=&Date_DaySuffix($d);
  3346.  
  3347. Add `st', `nd', `rd', `th' to a date (ie 1st, 22nd, 29th).  Works for
  3348. international dates.
  3349.  
  3350. =item Date_TimeZone
  3351.  
  3352.  $tz=&Date_TimeZone
  3353.  
  3354. This returns a timezone.  It looks in the following places for a timezone
  3355. in the following order:
  3356.  
  3357.    $ENV{TZ}
  3358.    $main::TZ
  3359.    unix 'date' command
  3360.    /etc/TIMEZONE
  3361.  
  3362. If it's not found in any of those places, an error occurs:
  3363.  
  3364.    ERROR: Date::Manip unable to determine TimeZone.
  3365.  
  3366. Date_TimeZone is able to read zones of the format PST8PDT (see TIMEZONES
  3367. documentation below).
  3368.  
  3369. =item Date_ConvTZ
  3370.  
  3371.  $date=&Date_ConvTZ($date,$from)
  3372.  $date=&Date_ConvTZ($date,$from,$to)
  3373.  
  3374. This converts a date (which MUST be in the format returned by ParseDate)
  3375. from one timezone to another.  The behavior of Date_ConvTZ depends on
  3376. whether it is called with 2 or 3 arguments.
  3377.  
  3378. If it is called with 2 arguments, $date is assumed to be in timezone given
  3379. in $from and it is converted to the timzone specified by the config
  3380. variable ConvTZ.  If ConvTZ is set to "IGNORE", no conversion is done and
  3381. $date is returned unmodified (see documentation on ConvTZ below).  This
  3382. form is most often used internally by the Date::Manip module.  The 3
  3383. argument form is of more use to most users.
  3384.  
  3385. If Date_ConvTZ is called with 3 arguments, the config variable ConvTZ is
  3386. ignored and $date is given in the timezone $from and is converted to the
  3387. timzone $to.  If $from is not given, it defaults to the working timezone.
  3388. NOTE: As in all other cases, the $date returned from Date_ConvTZ has no
  3389. timezone information included as part of it, so calling UnixDate with the
  3390. "%z" format will return the timezone that Date::Manip is working in
  3391. (usually the local timezone).
  3392.  
  3393. Example:  To convert 2/2/96 noon PST to CST (regardless of what timezone
  3394. you are in, do the following:
  3395.  
  3396.  $date=&ParseDate("2/2/96 noon");
  3397.  $date=&Date_ConvTZ($date,"PST","CST");
  3398.  
  3399. Both timezones MUST be in one of the formst listed below in the section
  3400. TIMEZONES.
  3401.  
  3402. =item Date_Init
  3403.  
  3404.  $flag=&Date_Init();
  3405.  $flag=&Date_Init("VAR=VAL","VAR=VAL",...);
  3406.  
  3407. Normally, it is not necessary to explicitely call Date_Init.  The first
  3408. time any of the other routines are called, Date_Init will be called to set
  3409. everything up.  If for some reason you want to change the configuration of
  3410. Date::Manip, you can pass the appropriate string or strings into Date_Init
  3411. to reinitizize things.
  3412.  
  3413. The strings to pass in are of the form "VAR=VAL".  Any number may be
  3414. included and they can come in any order.  VAR may be any configuration
  3415. variable.  A list of all configuaration variables is given in the section
  3416. CUSTOMIZING DATE::MANIP below.  VAL is any allowed value for that variable.
  3417. For example, to switch from English to French and use non-US format (so
  3418. that 12/10 is Oct 12), do the following:
  3419.  
  3420.   &Date_Init("Language=French","DateFormat=nonUS");
  3421.  
  3422. Note that the usage of Date_Init changed with version 5.07.  The old
  3423. calling convention is allowed but is depreciated.
  3424.  
  3425. If you change timezones in the middle of using Date::Manip, comparing dates
  3426. from before the switch to dates from after the switch will produce incorrect
  3427. results.
  3428.  
  3429. =item Date_IsWorkDay
  3430.  
  3431.   $flag=&Date_IsWorkDay($date [,$flag]);
  3432.  
  3433. This returns 1 if $date is a work day.  If $flag is non-zero, the time is
  3434. checked to see if it falls within work hours.
  3435.  
  3436. =item Date_NextWorkDay
  3437.  
  3438.   $date=&Date_NextWorkDay($date,$off [,$time]);
  3439.  
  3440. Finds the day $off work days from now.  If $time is passed in, we must also
  3441. take into account the time of day.
  3442.  
  3443. If $time is not passed in, day 0 is today (if today is a workday) or the
  3444. next work day if it isn't.  In any case, the time of day is unaffected.
  3445.  
  3446. If $time is passed in, day 0 is now (if now is part of a workday) or the
  3447. start of the very next work day.
  3448.  
  3449. =item Date_PrevWorkDay
  3450.  
  3451.   $date=&Date_PrevWorkDay($date,$off [,$time]);
  3452.  
  3453. Similar to Date_NextWorkDay.
  3454.  
  3455. =item DateManipVersion
  3456.  
  3457.   $version=&DateManipVersion
  3458.  
  3459. Returns the version of Date::Manip.
  3460.  
  3461. =back
  3462.  
  3463. =head1 TIMEZONES
  3464.  
  3465. The following timezone names are currently understood (and can be used in
  3466. parsing dates).  These are zones defined in RFC 822.
  3467.  
  3468.     Universal:  GMT, UT
  3469.     US zones :  EST, EDT, CST, CDT, MST, MDT, PST, PDT
  3470.     Military :  A to Z (except J)
  3471.     Other    :  +HHMM or -HHMM
  3472.  
  3473. In addition, the following timezone abbreviations are also accepted.  In a
  3474. few cases, the same abbreviation is used for two different timezones (for
  3475. example, NST stands for Newfoundland Standare -0330 and North Sumatra +0630).
  3476. In these cases, only 1 of the two is available.  The one preceded by a "#"
  3477. sign is NOT available but is documented here for completeness.  This list of
  3478. zones comes from the Time::Zone module by Graham Barr, David Muir Sharnoff,
  3479. and Paul Foley.
  3480.  
  3481.       IDLW    -1200    International Date Line West
  3482.       NT      -1100    Nome
  3483.       HST     -1000    Hawaii Standard
  3484.       CAT     -1000    Central Alaska
  3485.       AHST    -1000    Alaska-Hawaii Standard
  3486.       YST     -0900    Yukon Standard
  3487.       HDT     -0900    Hawaii Daylight
  3488.       YDT     -0800    Yukon Daylight
  3489.       PST     -0800    Pacific Standard
  3490.       PDT     -0700    Pacific Daylight
  3491.       MST     -0700    Mountain Standard
  3492.       MDT     -0600    Mountain Daylight
  3493.       CST     -0600    Central Standard
  3494.       CDT     -0500    Central Daylight
  3495.       EST     -0500    Eastern Standard
  3496.       EDT     -0400    Eastern Daylight
  3497.       AST     -0400    Atlantic Standard
  3498.       NFT     -0330    Newfoundland
  3499.       BST     -0300    Brazil Standard             bst=British Summer   +0100
  3500.       ADT     -0300    Atlantic Daylight
  3501.       NDT     -0230    Newfoundland Daylight
  3502.       AT      -0200    Azores
  3503.       WAT     -0100    West Africa
  3504.       GMT     +0000    Greenwich Mean
  3505.       UT      +0000    Universal (Coordinated)
  3506.       UTC     +0000    Universal (Coordinated)
  3507.       WET     +0000    Western European
  3508.       CET     +0100    Central European
  3509.       FWT     +0100    French Winter
  3510.       MET     +0100    Middle European
  3511.       MEWT    +0100    Middle European Winter
  3512.       SWT     +0100    Swedish Winter
  3513.       EET     +0200    Eastern Europe, USSR Zone 1
  3514.       FST     +0200    French Summer
  3515.       MEST    +0200    Middle European Summer
  3516.       SST     +0200    Swedish Summer              sst=South Sumatra    +0700
  3517.       BT      +0300    Baghdad, USSR Zone 2
  3518.       IT      +0330    Iran
  3519.       ZP4     +0400    USSR Zone 3
  3520.       ZP5     +0500    USSR Zone 4
  3521.       IST     +0530    Indian Standard
  3522.       ZP6     +0600    USSR Zone 5
  3523.       NST     +0630    North Sumatra               nst=Newfoundland Std -0330
  3524.       WAST    +0700    West Australian Standard
  3525.       JT      +0730    Java (3pm in Cronusland!)
  3526.       CCT     +0800    China Coast, USSR Zone 7
  3527.       WADT    +0800    West Australian Daylight
  3528.       JST     +0900    Japan Standard, USSR Zone 8
  3529.       CAST    +0930    Central Australian Standard
  3530.       EAST    +1000    Eastern Australian Standard
  3531.       GST     +1000    Guam Standard, USSR Zone 9  gst=Greenland Std    -0300
  3532.       CADT    +1030    Central Australian Daylight
  3533.       EADT    +1100    Eastern Australian Daylight
  3534.       IDLE    +1200    International Date Line East
  3535.       NZST    +1200    New Zealand Standard
  3536.       NZT     +1200    New Zealand
  3537.       NZDT    +1300    New Zealand Daylight
  3538.  
  3539. Others can be added in the future upon request.
  3540.  
  3541. DateManip needs to be able to determine the local timezone.  It can do this
  3542. by certain things such as the TZ environment variable (see Date_TimeZone
  3543. documentation above) or useing the TZ config variable (described below).
  3544. In either case, the timezone can be of the form STD#DST (for example
  3545. EST5EDT).  Both the standard and daylight savings time abbreviations must
  3546. be in the table above in order for this to work.  Also, this form may NOT
  3547. be used when parsing a date as there is no way to determine whether the
  3548. date is in daylight saving time or not.  The following forms are also
  3549. available and are treated similar to the STD#DST forms:
  3550.  
  3551.       US/Pacific
  3552.       US/Mountain
  3553.       US/Central
  3554.       US/Eastern
  3555.  
  3556. =head1 BUSINESS MODE
  3557.  
  3558. Anyone using business mode is going to notice a few quirks about it which
  3559. should be explained.  When I designed business mode, I had in mind what UPS
  3560. tells me when they say 2 day delivery, or what the local business which
  3561. promises 1 business day turnaround really means.
  3562.  
  3563. If you do a business day calculation (with the workday set to 9:00-5:00),
  3564. you will get the following:
  3565.  
  3566.    Saturday at noon + 1 business day = Tuesday at 9:00
  3567.    Saturday at noon - 1 business day = Friday at 9:00
  3568.  
  3569. What does this mean?
  3570.  
  3571. We have a business that works 9-5 and they have a drop box so I can drop
  3572. things off over the weekend and they promise 1 business day turnaround.  If
  3573. I drop something off Friday night, Saturday, or Sunday, it doesn't matter.
  3574. They're going to get started on it Monday morning.  It'll be 1 business day
  3575. to finish the job, so the earliest I can expect it to be done is around
  3576. 17:00 Monday or 9:00 Tuesday morning.  Unfortunately, there is some
  3577. ambiguity as to what day 17:00 really falls on, similar to the ambiguity
  3578. that occurs when you ask what day midnight falls on.  Although it's not the
  3579. only answer, Date::Manip treats midnight as the beginning of a day rather
  3580. than the end of one.  In the same way, 17:00 is equivalent to 9:00 the next
  3581. day and any time the date calculations encounter 17:00, it automatically
  3582. switch to 9:00 the next day.  Although this introduces some quirks, I think
  3583. this is justified.  You just have to treat 9:00 as being ambiguous (in the
  3584. same way you treat midnight as being ambiguous).
  3585.  
  3586. Equivalently, if I want a job to be finished on Saturday (despite the fact
  3587. that I cannot pick it up since the business is closed), I have to drop it
  3588. off no later than Friday at 9:00.  That gives them a full business day to
  3589. finish it off.  Of course, I could just as easily drop it off at 17:00
  3590. Thursday, or any time between then and 9:00 Friday.  Again, it's a matter
  3591. of treating 9:00 as ambiguous.
  3592.  
  3593. So, in case the business date calculations ever produce results that you
  3594. find confusing, I believe the solution is to write a wrapper which,
  3595. whenever it sees a date with the time of exactly 9:00, it treats it
  3596. specially (depending on what you want.
  3597.  
  3598. So Saturday + 1 business day = Tuesday at 9:00 (which means anything
  3599. from Monday 17:00 to Tuesday 9:00), but Monday at 9:01 + 1 business
  3600. day = Tuesday at 9:01 which is exact.
  3601.  
  3602. If this is not exactly what you have in mind, don't use the DateCalc
  3603. routine.  You can probably get whatever behavior you want using the
  3604. routines Date_IsWorkDay, Date_NextWorkDay, and Date_PrevWorkDay described
  3605. above.
  3606.  
  3607. =head1 CUSTOMIZING DATE::MANIP
  3608.  
  3609. There are a number of variables which can be used to customize the way
  3610. Date::Manip behaves.  There are also several ways to set these variables.
  3611.  
  3612. At the top of the Manip.pm file, there is a section which contains all
  3613. customization variables.  These provide the default values.
  3614.  
  3615. These can be overridden in a global config file if one is present (this
  3616. file is optional).  If the GlobalCnf variable is set in the Manip.pm file,
  3617. it contains the full path to a config file.  If the file exists, it's
  3618. values will override those set in the Manip.pm file.  A sample config file
  3619. is included with the Date::Manip distribution.  Modify it as appropriate
  3620. and copy it to some appropriate directory and set the GlobalCnf variable
  3621. in the Manip.pm file.
  3622.  
  3623. Each user can have a personal config file which is of the same form as
  3624. the global config file.  The variables PersonalCnf and PersonalCnfPath
  3625. set the name and search path for the personal config file.
  3626.  
  3627. Finally, any variables passed in through Date_Init override all other
  3628. values.
  3629.  
  3630. A config file can be composed of several sections (though only 2 of them
  3631. are currently used).  The first section sets configuration varibles.  Lines
  3632. in this section are of the form:
  3633.  
  3634.    VARIABLE = VALUE
  3635.  
  3636. For example, to make the default language French, include the line:
  3637.  
  3638.    Language = French
  3639.  
  3640. Only variables described below may be used.  Blank lines and lines beginning
  3641. with a pound sign (#) are ignored.  All spaces are optional and strings are
  3642. case insensitive.
  3643.  
  3644. A line which starts with an asterix (*) designates a new section.  The only
  3645. section currently used is the Holiday section.  All lines are of the form:
  3646.  
  3647.    DATE = HOLIDAY
  3648.  
  3649. HOLIDAY is the name of the holiday (or it can be blank in which case the
  3650. day will still be treated as a holiday... for example the day after
  3651. Thanksgiving or Christmas is often a work holiday though neither are
  3652. named).
  3653.  
  3654. DATE is a string which can be parsed to give a valid date in any year.  It
  3655. can be of the form
  3656.  
  3657.    Date
  3658.    Date + Delta
  3659.    Date - Delta
  3660.  
  3661. A valid holiday section would be:
  3662.  
  3663.    *Holiday
  3664.  
  3665.    1/1                             = New Year's Day
  3666.    third Monday in Feb             = Presidents' Day
  3667.    fourth Thu in Nov               = Thanksgiving
  3668.  
  3669.    fourth Thu in Nov + 1 day       =
  3670.  
  3671. In a Date + Delta or Date - Delta string, you can use business mode by
  3672. including the appropriate string (see documentation on DateCalc) in the
  3673. Date or Delta.  So (in English), the first workday before Christmas could
  3674. be defined as:
  3675.  
  3676.    12/25 - 1 business day          =
  3677.  
  3678. All Date::Manip variables which can be used are described in the following
  3679. section.
  3680.  
  3681. =over 4
  3682.  
  3683. =item IgnoreGlobalCnf
  3684.  
  3685. If this variable is used (any value is ignored), the global config file
  3686. is not read.  It must be present in the initial call to Date_Init or the
  3687. global config file will be read.
  3688.  
  3689. =item EraseHolidays
  3690.  
  3691. If this variable is used (any value is ignored), the current list of
  3692. defined holidays is erased.  A new set will be set the next time a
  3693. config file is read in.
  3694.  
  3695. =item PersonalCnf
  3696.  
  3697. This variable can be passed into Date_Init to read a different personal
  3698. configuration file.  It can also be included in the global config file
  3699. to define where personal config files live.
  3700.  
  3701. =item PersonalCnfPath
  3702.  
  3703. Used in the same way as the PersonalCnf option.  You can use tilde (~)
  3704. expansions when defining the path.
  3705.  
  3706. =item Language
  3707.  
  3708. Date::Manip can be used to parse dates in many different languages.
  3709. Currently, it is configured to read English, Swedish, and French dates,
  3710. but others can be added easily.  Language is set to the language used to
  3711. parse dates.
  3712.  
  3713. =item DateFormat
  3714.  
  3715. Different countries look at the date 12/10/96 as Dec 10 or Oct 12.  In the
  3716. United States, the first is most common, but this certainly doesn't hold
  3717. true for other countries.  Setting DateFormat to "US" forces the first
  3718. behavior (Dec 10).  Setting DateFormat to anything else forces the second
  3719. behavior (Oct 12).
  3720.  
  3721. =item TZ
  3722.  
  3723. Date::Manip is able to understand some timezones (and others will be added
  3724. in the future).  At the very least, all zones defined in RFC 822 are
  3725. supported.  Currently supported zones are listed in the TIMEZONES section
  3726. above and all timezones should be entered as one of them.
  3727.  
  3728. Date::Manip must be able to determine the timezone the user is in.  It does
  3729. this by looking in the following places:
  3730.  
  3731.    the environment variable TZ
  3732.    the variable $main::TZ
  3733.    the file /etc/TIMEZONE
  3734.    the 5th element of the unix "date" command (not available on NT machines)
  3735.  
  3736. At least one of these should contain a timezone in one of the supported
  3737. forms.  If it doesn't, the TZ variable must be set to contain the local
  3738. timezone in the appropriate form.
  3739.  
  3740. The TZ variable will override the other methods of determining the
  3741. timezone, so it should probably be left blank if any of the other methods
  3742. will work.  Otherwise, you will have to modify the variable every time you
  3743. switch to/from daylight savings time.
  3744.  
  3745. =item ConvTZ
  3746.  
  3747. All date comparisons and calculations must be done in a single time zone in
  3748. order for them to work correctly.  So, when a date is parsed, it should be
  3749. converted to a specific timezone.  This allows dates to easily be compared
  3750. and manipulated as if they are all in a single timezone.
  3751.  
  3752. The ConvTZ variable determines which timezone should be used to store dates
  3753. in.  If it is left blank, all dates are converted to the local timezone
  3754. (see the TZ variable above).  If it is set to one of the timezones listed
  3755. above, all dates are converted to this timezone.  Finally, if it is set to
  3756. the string "IGNORE", all timezone information is ignored as the dates are
  3757. read in (in this case, the two dates "1/1/96 12:00 GMT" and "1/1/96 12:00
  3758. EST" would be treated as identical).
  3759.  
  3760. =item Internal
  3761.  
  3762. When a date is parsed using ParseDate, that date is stored in an internal
  3763. format which is understood by the Date::Manip routines UnixDate and
  3764. DateCalc.  Originally, the format used to store the date internally was:
  3765.  
  3766.    YYYYMMDDHH:MN:SS
  3767.  
  3768. It has been suggested that I remove the colons (:) to shorten this to:
  3769.  
  3770.    YYYYMMDDHHMNSS
  3771.  
  3772. The main advantage of this is that some databases are colon delimited which
  3773. makes storing date from Date::Manip tedious.
  3774.  
  3775. In order to maintain backwards compatibility, the Internal varialbe was
  3776. introduced.  Set it to 0 (to use the old format) or 1 (to use the new
  3777. format).
  3778.  
  3779. =item FirstDay
  3780.  
  3781. It is sometimes necessary to know what day of week is regarded as first.
  3782. By default, this is set to sunday, but many countries and people will
  3783. prefer monday (and in a few cases, a different day may be desired).  Set
  3784. the FirstDay variable to be the first day of the week (0=sunday to
  3785. 6=saturday).  Incidentally, monday should be chosen as the default to be in
  3786. complete accordance with ISO 8601.
  3787.  
  3788. =item WorkWeekBeg, WorkWeekEnd
  3789.  
  3790. The first and last days of the work week.  By default, monday and friday.
  3791. WorkWeekBeg must come before WorkWeekEnd numerically.  The days are
  3792. numbered from 0 (sunday) to 6 (saturday).  There is no way to handle an odd
  3793. work week of Thu to Mon for example.
  3794.  
  3795. =item WorkDay24Hr
  3796.  
  3797. If this is non-nil, a work day is treated as being 24 hours long.  The
  3798. WorkDayBeg and WorkDayEnd variables are ignored in this case.
  3799.  
  3800. =item WorkDayBeg, WorkDayEnd
  3801.  
  3802. The times when the work day starts and ends.  WorkDayBeg must come before
  3803. WorkDayEnd (i.e. there is no way to handle the night shift where the work
  3804. day starts one day and ends another).  Also, the workday MUST be more than
  3805. one hour long (of course, if this isn't the case, let me know... I want a
  3806. job there!).
  3807.  
  3808. The time in both can be in any valid time format (including international
  3809. formats), but seconds will be ignored.
  3810.  
  3811. =item DeltaSigns
  3812.  
  3813. Prior to Date::Manip version 5.07, a negative delta would put negative
  3814. signs in front of every component (i.e. "0:0:-1:-3:0:-4").  By default,
  3815. 5.07 changes this behavior to print only 1 or two signs in front of the
  3816. year and day elements (even if these elements might be zero) and the sign
  3817. for year/month and day/hour/minute/second are the same.  Setting this
  3818. variable to non-zero forces deltas to be stored with a sign in front of
  3819. every element (including elements equal to 0).
  3820.  
  3821. =back
  3822.  
  3823. =head1 BACKWARDS INCOMPATIBILITIES
  3824.  
  3825. For the most part, Date::Manip has remained backward compatible at every
  3826. release.  There have been a few minor incompatibilities introduced at
  3827. various stages.
  3828.  
  3829. Version 5.07 introduced 2 minor incompatibilities.  In the UnixDate
  3830. command, the "%s" format changed.  In version 5.06, "%s" returned the
  3831. number of seconds since Jan 1, 1970 in the current timezone.  In 5.07,
  3832. it returns the number of seconds since Jan 1, 1970 GMT.  The "%o" format
  3833. was added to return what "%s" previously did.
  3834.  
  3835. Also in 5.07, the format for the deltas returned by ParseDateDelta changed.
  3836. Previously, each element of a delta had a sign attached to it
  3837. (+1:+2:+3:+4:+5:+6).  The new format removes all unnecessary signs by
  3838. default (+1:2:3:4:5:6).  Also, because of the way deltas are normalized
  3839. (see documentation on ParseDateDelta), at most two signs are included.
  3840. For backwards compatibility, the config variable DeltaSigns was added.  If
  3841. set to 1, all deltas include all 6 signs.
  3842.  
  3843. Finally, in 5.07 the format of the Date_Init calling arguments changed.  The
  3844. old method
  3845.  
  3846.   &Date_Init($language,$format,$tz,$convtz);
  3847.  
  3848. is still supported, but this support will likely disappear in the future.
  3849. Use the new calling format instead:
  3850.  
  3851.   &Date_Init("var=val","var=val",...);
  3852.  
  3853. One more important incompatibility is projected for ParseDate in the next
  3854. major release of Date::Manip.  The next release will support full ISO 8601
  3855. date formats including the format YY-MM-DD.  The current version of
  3856. ParseDate supports the format MM-DD-YY, which is commonly used in the US,
  3857. but is not part of any standard.  Unfortunately, there is no way to
  3858. unambiguously look at a date of the format XX-XX-XX and determine whether
  3859. it is YY-MM-DD or MM-DD-YY.  As a result, the MM-DD-YY format will no
  3860. longer be supported in favor of the YY-MM-DD format.  The MM/DD/YY and
  3861. MM-DD-YYYY formats WILL still be supported!
  3862.  
  3863. =head1 COMMON PROBLEMS
  3864.  
  3865. Perhaps the most common problem occurs when you get the error:
  3866.  
  3867.    Error: Date::Manip unable to determine TimeZone.
  3868.  
  3869. Date::Manip tries hard to determine the local timezone, but on some
  3870. machines, it cannot do this (especially those without a unix date
  3871. command... i.e. Microsoft Windows systems).  To fix this, just set the TZ
  3872. variable, either at the top of the Manip.pm file, or in the DateManip.cnf
  3873. file.  I suggest using the form "EST5EDT" so you don't have to change it
  3874. every 6 months when going to or from daylight savings time.
  3875.  
  3876. =head1 KNOWN PROBLEMS
  3877.  
  3878. =over 4
  3879.  
  3880. =item Daylight Savings Times
  3881.  
  3882. Date::Manip does not handle daylight savings time, though it does handle
  3883. timezones to a certain extent.  Converting from EST to PST works fine.
  3884. Going from EST to PDT is unreliable.
  3885.  
  3886. The following examples are run in the winter of the US East coast (i.e.
  3887. in the EST timezone).
  3888.  
  3889.     print UnixDate(ParseDate("6/1/97 noon"),"%u"),"\n";
  3890.         => Sun Jun  1 12:00:00 EST 1997
  3891.  
  3892. June 1 EST does not exist.  June 1st is during EDT.  It should print:
  3893.  
  3894.         => Sun Jun  1 00:00:00 EDT 1997
  3895.  
  3896. Even explicitely adding the timezone doesn't fix things (if anything, it
  3897. makes them worse):
  3898.  
  3899.     print UnixDate(ParseDate("6/1/97 noon EDT"),"%u"),"\n";
  3900.         => Sun Jun  1 11:00:00 EST 1997
  3901.  
  3902. Date::Manip converts everything to the current timezone (EST in this case).
  3903.  
  3904. Related problems occur when trying to do date calculations over a timezone
  3905. change.  These calculations may be off by an hour.
  3906.  
  3907. Also, if you are running a script which uses Date::Manip over a period of
  3908. time which starts in one time zone and ends in another (i.e. it switches
  3909. form Daylight Savings Time to Standard Time or vice versa), many things may
  3910. be wrong (especially elapsed time).
  3911.  
  3912. I hope to fix these problems in the next release so that it would convert
  3913. everything to the current zones (EST or EDT).
  3914.  
  3915. =item Sorting Problems
  3916.  
  3917. If you use Date::Manip to sort a number of dates, you must call Date_Init
  3918. either explicitely, or by way of some other Date::Manip routine before it
  3919. is used in the sort.  For example, the following code fails:
  3920.  
  3921.    use Date::Manip;
  3922.    sub sortDate {
  3923.        my($date1, $date2);
  3924.        $date1 = &ParseDate($a);
  3925.        $date2 = &ParseDate($b);
  3926.        return ($date1 cmp $date2);
  3927.    }
  3928.    @date = ("Fri 16 Aug 96",
  3929.             "Mon 19 Aug 96",
  3930.             "Thu 15 Aug 96");
  3931.    @i=sort sortDate @dates;
  3932.  
  3933. but if you uncomment the Date_Init line, it works.  The reason for this is
  3934. that the first time you call Date_Init, it initializes a number of items
  3935. used by Date::Manip.  Some of these are sorted.  It turns out that perl
  3936. (5.003 and earlier) has a bug in it which does not allow a sort within a
  3937. sort.  The next version (5.004) may fix this.  For now, the best thing to
  3938. do is to call Date_Init explicitely.  NOTE: This is an extremely
  3939. inefficient way to sort data.  Instead, you should translate the dates to
  3940. the Date::Manip internal format, sort them using a normal string
  3941. comparison, and then convert them back to the format desired using
  3942. UnixDate.
  3943.  
  3944. =item RCS Control
  3945.  
  3946. If you try to put Date::Manip under RCS control, you are going to have
  3947. problems.  Apparently, RCS replaces strings of the form "$Date...$" with
  3948. the current date.  This form occurs all over in Date::Manip.  Since very
  3949. few people will ever have a desire to do this (and I don't use RCS), I have
  3950. not worried about it.
  3951.  
  3952. =back
  3953.  
  3954. =head1 AUTHOR
  3955.  
  3956. Sullivan Beck (beck@qtp.ufl.edu)
  3957.  
  3958. =cut
  3959.